{"id":17037221,"url":"https://github.com/bast/contain-r","last_synced_at":"2026-04-26T08:39:34.097Z","repository":{"id":162995165,"uuid":"638237664","full_name":"bast/contain-R","owner":"bast","description":"Apptainer/Singularity container for reproducible R environments.","archived":false,"fork":false,"pushed_at":"2023-05-09T16:11:51.000Z","size":15,"stargazers_count":5,"open_issues_count":4,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-27T21:41:59.717Z","etag":null,"topics":["apptainer","r","renv","singularity"],"latest_commit_sha":null,"homepage":"","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bast.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-05-09T11:17:39.000Z","updated_at":"2025-09-25T16:20:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"7015aa0c-f3a9-4ee2-acef-240780b3b895","html_url":"https://github.com/bast/contain-R","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/bast/contain-R","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fcontain-R","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fcontain-R/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fcontain-R/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fcontain-R/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bast","download_url":"https://codeload.github.com/bast/contain-R/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bast%2Fcontain-R/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32291337,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T08:29:33.829Z","status":"ssl_error","status_checked_at":"2026-04-26T08:29:18.366Z","response_time":129,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apptainer","r","renv","singularity"],"created_at":"2024-10-14T08:53:11.342Z","updated_at":"2026-04-26T08:39:34.091Z","avatar_url":"https://github.com/bast.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# contain-R\n\nApptainer/Singularity container for reproducible R environments.\n\n\n## What you need for this to work\n\n- [Apptainer](https://apptainer.org/) or [Singularity CE](https://docs.sylabs.io/guides/latest/user-guide/introduction.html)\n- `install.R` or `renv.lock` file (examples below) that define the environment\n- An R script/project/command that you want to run in that environment\n- **No need to install R itself** (R 4.3.0 is provided by the container)\n\n\n## Motivation and big picture\n\nFor reproducibility it is important to:\n- document dependencies\n- isolate dependencies from dependencies of other projects\n\nThis container:\n- creates a per-project [renv](https://rstudio.github.io/renv/)-environment and\n  isolates dependencies\n- uses [pak](https://pak.r-lib.org/) under\n  the hood to speed up installation\n- allows to configure a user- or group-wide cache which can be reused across projects\n- does not allow accidental \"I will just quickly install it into my system and document it later\" since it is a container\n- forces you to document your dependencies which is good for reproducibility and your future self\n\nDependencies are not installed into the container but only managed by the container.\n\n\n## Quick start on your computer\n\n1. Create a new directory.\n2. In the new directory create a file `install.R` which contains:\n```r\nrenv::install('ggplot2')\n```\n3. Download the container:\n```bash\n$ singularity pull https://github.com/bast/contain-R/releases/download/0.1.0/contain-R.sif\n```\n4. Run the following in your terminal (it starts installing stuff; this takes 1-2 minutes on my computer):\n```bash\n$ ./contain-R.sif R --quiet -e 'library(ggplot2)'\n```\n5. Run the above again (now it will only take a second).\n6. Run some R script which depends on that environment:\n```bash\n$ ./contain-R.sif Rscript somescript.R\n```\n7. Or if you want the R interactive shell:\n```bash\n$ ./contain-R.sif R\n```\n\n\n## Quick start on a cluster\n\nSame as above but instead of steps 3 and 4, use the following and adapt paths to your\nsituation:\n```bash\n# probably you do not want to be in your home folder to not fill your disk quota\ncd /cluster/work/users/myself/experiment\n\n# download the container\n$ singularity pull https://github.com/bast/contain-R/releases/download/0.1.0/contain-R.sif\n\n# you decide where these should go\nexport RENV_CACHE=/cluster/work/users/myself/renv-cache\nexport PAK_CACHE=/cluster/work/users/myself/pak-cache\n\n# you need only one of the two\nexport SINGULARITY_BIND=\"/cluster\"\nexport APPTAINER_BIND=\"/cluster\"\n\n./contain-R.sif R --quiet -e 'library(ggplot2)'\n```\n\n\n## install.R or renv.lock or both?\n\nYou need something to define the environment you want, either `install.R` or `renv.lock`.\n\nAn `install.R` file looks like this:\n```r\nrenv::install('ggplot2')\nrenv::install('vcfR')\nrenv::install('hierfstat')\nrenv::install('poppr')\n```\n\nList as many packages as you need. You can pin them to specific versions, if\nneeded:\n```r\nrenv::install(\"digest@0.6.18\")\n```\n\nAlternatively, you can create your environment from `renv.lock` which looks\nlike this example and typically has been generated by [renv](https://rstudio.github.io/renv/):\n```json\n{\n  \"R\": {\n    \"Version\": \"3.6.1\",\n    \"Repositories\": [\n      {\n        \"Name\": \"CRAN\",\n        \"URL\": \"https://cloud.r-project.org\"\n      }\n    ]\n  },\n  \"Packages\": {\n    \"markdown\": {\n      \"Package\": \"markdown\",\n      \"Version\": \"1.0\",\n      \"Source\": \"Repository\",\n      \"Repository\": \"CRAN\",\n      \"Hash\": \"4584a57f565dd7987d59dda3a02cfb41\"\n    },\n    \"mime\": {\n      \"Package\": \"mime\",\n      \"Version\": \"0.7\",\n      \"Source\": \"Repository\",\n      \"Repository\": \"CRAN\",\n      \"Hash\": \"908d95ccbfd1dd274073ef07a7c93934\"\n    }\n  }\n}\n```\nFor more information about lock files, please see\n\u003chttps://rstudio.github.io/renv/reference/lockfiles.html\u003e.\n\nThe container will process them in this order:\n- If there is only `install.R`, it will use that one and create an `renv` environment and lock dependencies in `renv.lock`.\n- If there is only `renv.lock`, it will use that one and create an `renv` environment.\n- If `install.R` is more recent than `renv`, it will install from it (again).\n- If `renv.lock` is more recent than `renv`, it will install from it (again).\n\nIn practice you will probably do either of these two:\n- You arrive with `install.R` and it will create `renv.lock` and `renv`. You\n  can then take the `renv.lock` and use it to share an environment with your\n  friend. Maybe you modify `install.R` later and refresh `renv.lock` and `renv`.\n- Or you arrive with `renv.lock` that you got from somebody and it will create\n  `renv`.\n\n\n## Generated paths\n\nRunning the container creates the following files and directories in the same\nplace where you run the container (but you can configure some of them if you\nwant them somewhere else):\n- `renv` - holding the environment\n- `renv.lock` - created or updated if you installed from `install.R`\n- creates or modifies `.Rprofile` - [renv](https://rstudio.github.io/renv/) adds the line `source(\"renv/activate.R\")`\n- `renv-cache` - [renv](https://rstudio.github.io/renv/) package cache; you can change its location by defining environment variable `RENV_CACHE`\n- `pak-cache` - [pak](https://pak.r-lib.org/) package cache; you can change its location by defining environment variable `RENV_CACHE`\n\n\n## Installation takes too long?\n\nRunning a script for the first time may take time since it needs to set up the\nenvironment and download and install dependencies.\n\nHowever, re-running the script will take no installation time and if\ndependencies are already in the cache, it will take no time either.\n\n\n## Pak and renv use different caches and methods\n\nFor historical reasons they are slightly different but their\ndevelopers are working on smoothing things out between the two.\nYou will notice the difference if you start from `install.R`,\nand then try to restore back from the generated `renv.lock`: you will\nnotice that the two will use different methods.\n\nRelevant GitHub issues:\n- https://github.com/rstudio/renv/issues/907\n- https://github.com/r-lib/pak/issues/343\n\nYou have the option to turn off [pak](https://pak.r-lib.org/) like this:\n```bash\nexport USE_PAK=false\n```\n\nPros and cons of turning it off:\n- Advantage: Only one cache location and everything is nicely consistent. You\n  could install from `install.R`, then remove it even and run from `renv.lock`\n  and it would be all consistent and not need to re-install anything.\n- Disadvantage: First installation from `install.R` might take longer when\n  without [pak](https://pak.r-lib.org/).\n\n\n## How to configure location for package caches\n\nYou can change the location of the package caches:\n```bash\nexport RENV_CACHE=/home/user/R/renv-cache\nexport PAK_CACHE=/home/user/R/pak-cache\n```\n\n\n## Recommendations on where to place package caches\n\n**On your own computer** it will make sense to reuse the same cache(s) across\nall projects.  This way, when installing dependencies, renv will first look\nwhether you already have the package on your computer.\n\n**On a shared cluster** it might make sense to have one common cache for your\ngroup/allocation since your research group might use similar dependencies in\ntheir work.  This way you can save space and install time.\n\n\n## Known problems/ ideas for later\n\n- Maybe you need a different version of R than 4.3.0. I guess we should at some\n  point have several containers for different versions? Or you build your own\n  from the definition file.\n- It could be good to let the user configure where `renv` itself should be\n  located. Currently it is placed in the same folder where the container is run.\n\n\n## Resources\n\nI have used these resources when writing/testing:\n- https://rstudio.github.io/renv/\n- https://rstudio.github.io/renv/articles/docker.html\n- https://pak.r-lib.org/\n- https://rstudio.github.io/packrat/ (deprecated)\n- https://sites.google.com/nyu.edu/nyu-hpc/hpc-systems/greene/software/r-packages-with-renv\n- https://raps-with-r.dev/repro_intro.html\n- https://www.youtube.com/watch?v=N7z1K4FhVFE (stream recording on how to use renv)\n- https://github.com/singularityhub/singularity-deploy\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbast%2Fcontain-r","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbast%2Fcontain-r","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbast%2Fcontain-r/lists"}