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

        • Need to store docker images with all the libraries associated with them. (Thread)
        • Explicitly install sf from source and from CRAN. This will be recorded in the lockfile and the next time you `renv::restore()` your project, only sf and its dependencies will be re-compiled. (Thread)
  • Bruno Rodrigues posts on reproducibility

  • 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
  • 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

      renv::lockfile_modify(repos = c(
        P3M = "https://packagemanager.posit.co/cran/latest"
        )) |> 
        renv::lockfile_write()
      • 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"
  • Install from github

    renv::install("eddelbuettel/digest")

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.
    • https://rstudio.github.io/renv/articles/faq.html#im-returning-to-an-older-renv-project-what-do-i-do
  • Init steps (updates lockfile to either latest pkg versions or what you have locally)
    1. Run renv::init()
    2. Choose “Discard the lockfile and re-initialize the project”
  • Restore steps (looking to reproduce the original project results)
    1. Run renv::restore()
    2. 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)
  • 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):
        1. update("pkg") (or go to the github and search for the latest stable version) to see what the latest stable version is.
        2. renv::modify() allows you edit the lockfile. Change pkg version in lockfile to that latest version
        3. rerun renv::restore()
        4. Repeat as necessary

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
    1. Install {systemfonts}
    2. Run systemfonts::match_font("<name of font you have installed>") and copy path (don’t include file (i.e. .ttf))
    3. 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
  • 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

        remotes::install_version("cachem", version = "1.0.3", repos = "http://cran.us.r-project.org")
        renv::install("cachem@1.0.3")
  • Compiling older packages (e.g. {gfortran}) on Apple Silicon (Mac M1, M2, M3) can result in errors. {macrtools} can help some of them.