renv

repository·main·Indexed 22 days ago

https://github.com/rstudio/renv

An R package for creating reproducible, isolated, and portable environments for R projects. It manages private project libraries and dependency lockfiles (renv.lock) using core functions such as renv::init(), renv::snapshot(), renv::restore(), and renv::status().

Tokens
1K
Snippets
5
Records
6
Agent score
78%

What's inside renv

  1. How renv works: The core workflow

    main

    The renv workflow revolves around managing a project library and a lockfile (renv.lock).

    1. Initialize: Use renv::init() to create a private project library and a .Rprofile that ensures this library is used whenever the project is opened.
    2. Manage Packages: Install or upgrade packages using standard R functions like install.packages() and update.packages(), or renv specific functions like renv::install() and renv::update().
    3. Record State: Use renv::snapshot() to update the renv.lock file with the exact package versions and sources currently installed in your project library.
    4. Restore State: Use renv::restore() to install the specific package versions recorded in the renv.lock file into a project library. This is used when moving to a new machine or sharing code with collaborators.
    5. Verify: Use renv::status() to compare the current state of the project library against the metadata in the lockfile.
    # Typical workflow sequence
    renv::init()
    renv::install("somepackage")
    renv::snapshot()
    # On another machine:
    renv::restore()
  2. Install renv

    main

    You can install the latest stable version of renv from CRAN, or install the development version from r-universe.

    # Install stable version from CRAN
    install.packages("renv")
    
    # Install development version from r-universe
    install.packages("renv", repos = "https://rstudio.r-universe.dev")
  3. Configure synchronized check via REN_CONFIG_SYNCHRONIZED_CHECK

    main

    The environment variable RENV_CONFIG_SYNCHRONIZED_CHECK controls whether a synchronized check is performed. When running the renv CLI, if this variable is not explicitly set, it defaults to FALSE.

    # To enable the synchronized check via the CLI
    export RENV_CONFIG_SYNCHRONIZED_CHECK=TRUE
    renv snapshot
  4. Core renv functions and concepts

    main

    The following functions are the primary tools for managing your project environment:

    • renv::init(): Initializes renv in a new or existing project, creating a project library and a .Rprofile.
    • renv::snapshot(): Updates the renv.lock file based on the packages currently installed in the project library.
    • renv::restore(): Reinstalls the package versions specified in the renv.lock file into the project library.
    • renv::status(): Compares the current project library against the renv.lock file to identify discrepancies.
    • renv::install(): Installs packages into the project library.
    • renv::update(): Updates packages in the project library.

    Key abstractions:

    • Project Library: A private, isolated directory of R packages specific to your project.
    • Lockfile (renv.lock): A JSON file that records the exact versions and sources of all dependencies used in the project, ensuring reproducibility.
  5. Run renv commands via the Windows CLI

    main

    On Windows, you can execute renv commands directly from the terminal using the renv.bat entrypoint. This script invokes the R interpreter to run the renv:::renv_cli_exec() function, passing all provided command-line arguments to the renv CLI.

    Note that this execution environment sets RENV_CONFIG_SYNCHRONIZED_CHECK=FALSE by default.

    renv.bat <command> [args]
  6. Execute renv commands via the CLI

    main

    The renv executable allows you to run renv commands directly from your terminal. It acts as a wrapper that invokes R and executes the internal renv:::renv_cli_exec() function with the arguments you provide. This is useful for automating renv tasks in shell scripts or CI/CD pipelines without manually entering an R session.

    # Example: Running an renv command from the terminal
    renv snapshot
    
    # Example: Initializing renv in a project
    renv init