Renv
Misc
Also see
- Caveats page in the docs lists things that renv does not cover.
R version, Pandoc, Operating system, versions of system libraries, compiler versions
Potential solutions for packages like {sf} versions that require certain versions of system libraries
- Caveats page in the docs lists things that renv does not cover.
Bruno Rodrigues posts on reproducibility
- MRAN is getting shutdown - what else is there for reproducibility with R, or why reproducibility is on a continuum?
- tl;dr:
- I want to start a project and make it reproducible.
- {renv} and Docker (Vignette)
- There’s an old script laying around that I want to run.
- {groundhog} and Docker
- I want to work inside an environment that enables me to run code in a reproducible way.
- Docker and the Posit CRAN mirror.
- I want to start a project and make it reproducible.
- tl;dr:
- Code longevity of the R programming language
- MRAN is getting shutdown - what else is there for reproducibility with R, or why reproducibility is on a continuum?
Alternatives
- GNU Guix
- See Bruno’s “MRAN is getting shutdown - what else is there for reproducibility with R, or why reproducibility is on a continuum?” post for his thoughts
- GNU Guix
Posit Package Manager - Helps you install binary packages from a certain date
Notes from Ease renv::restore() by updating your repository to Posit Public Package Manager
e.g. Adding to .Rprofile “
options(repos = c(CRAN = "https://packagemanager.posit.co/cran/2024-02-06"))
- Might be able to keep a separate .Rprofie in the project directory.
Always installing package binaries from Posit Package Manager can help reduce reproducibility errors when it comes to restoring a project.
- When {renv} attempts to install that older package from CRAN, the packages will likely have to compile from source which takes longer and may fail for various reasons.
Update the repositories specified in the
renv.lock
::lockfile_modify(repos = c( renvP3M = "https://packagemanager.posit.co/cran/latest" |> )) ::lockfile_write() renv
- The P3M url specified here works for both macOS and Windows (not Linux).
- Can also include other repos
- R-Universe:
ropensci = "https://ropensci.r-universe.dev",
- Company/Organization:
org_rspm = "http://rspm.xxxx.org/xxxREPO/latest"
- R-Universe:
Install from github
::install("eddelbuettel/digest") renv
Activating/Restoring a Project
- Misc
- ** Restoring an environment comes with a few caveats: **
- First of all, renv does not install a different version of R if the recorded and current version disagree. This is a manual step and up to the user.
- {rig} can make switching between R versions easy
- Open RStudio with the correct version of R that’s listed in the renv.lock file:
rig rstudio path/to/renv.lock
- The same is true for packages with external dependencies. Those libraries, their headers and binaries also need to be installed by the user in the correct version, which is not recorded in the lockfile.
- Furthermore renv supports restoring packages installed from git repositories, but fails if the user did not install git beforehand.
- First of all, renv does not install a different version of R if the recorded and current version disagree. This is a manual step and up to the user.
- https://rstudio.github.io/renv/articles/faq.html#im-returning-to-an-older-renv-project-what-do-i-do
- ** Restoring an environment comes with a few caveats: **
- Init steps (updates lockfile to either latest pkg versions or what you have locally)
- Run
renv::init()
- Choose “Discard the lockfile and re-initialize the project”
- Run
- Restore steps (looking to reproduce the original project results)
- Run
renv::restore()
- Update any packages with outside dependencies
- RMarkdown depends on pandoc which is usually installed by installing RStudio. So if you updated RStudio, then the rmarkdown version in the project’s lockfile may not work with your current pandoc version. You’ll have to update rmarkdown (or revert your pandoc version… shhhyea, as if)
- Run
- Issues
- A pkg installation fails (and therefore restore fails) because some pkg requires another pkg to be a more up-to-date version or some other reason
- **If it’s something with a shit ton of dependencies like rstanarm, you might as well use renv::init( ) method**
- Examples:
- {pkgload} failed to install because I had rlang v.4.0.6 instead of > v4.09
- {RCurl} failed because it couldn’t find some obsure file
- Solution (update the package):
update("pkg")
(or go to the github and search for the latest stable version) to see what the latest stable version is.renv::modify()
allows you edit the lockfile. Change pkg version in lockfile to that latest versionrerun renv::restore()
- Repeat as necessary
- A pkg installation fails (and therefore restore fails) because some pkg requires another pkg to be a more up-to-date version or some other reason
Installing Fonts
- Using a package like extrafont, it won’t find any fonts installed so you have to point it to the system path
- Steps
- Install {systemfonts}
- Run
systemfonts::match_font("<name of font you have installed>")
and copy path (don’t include file (i.e. .ttf)) - Run
extrafont::
Using Local directory with Package Tarbell to Install a Package
Needed to install an old XML package version that wasn’t available through CRAN mirror. Errored looking libxml parser. Discussion says you need to use libxml2.
Resources
- renv using local package directory
- https://rstudio.github.io/renv/articles/local-sources.html
- Instructions on compiling old XML package with rtools40
- https://github.com/r-windows/rtools-installer/issues/3
- https://github.com/r-windows/checks/issues/5#issue-335598042
- Install libxml2 library
- https://github.com/r-windows/docs/blob/master/packages.md#xml
- Finding pacman package manager to install libxml2
- https://github.com/r-windows/docs/blob/master/rtools40.md#readme
- renv using local package directory
In the project directory, create renv/local directory. Then, download/move the package version’s tar.gz file to that “local” folder
> Sys.setenv(LIB_XML = "$(MINGW_PREFIX)") > Sys.setenv(LOCAL_CPPFLAGS = "-I/mingw$(WIN)/include/libxml2") > install.packages('renv/local/XML_3.99-0.3.tar.gz', repos = NULL, type = 'source')
Errors
- Fails to retrieve package
- Solutions:
Install from github
renv::install("eddelbuettel/digest")
Revert to previous version
::install_version("cachem", version = "1.0.3", repos = "http://cran.us.r-project.org") remotes::install("cachem@1.0.3") renv
- Solutions:
- Compiling older packages (e.g. {gfortran}) on Apple Silicon (Mac M1, M2, M3) can result in errors. {macrtools} can help some of them.