install.packages("renv")
::init() renv
Set up renv in R
renv
renv is an R package that helps you create and manage isolated project environments. It captures the specific package versions used in a project as snapshots, ensuring that your code runs consistently over time and across systems. It enables reproducible research and easy collaboration, similar to Python’s virtualenv.
Initialize and activate a new renv environment
Initializing an renv is simple. Once the package is installed simply call the following command.
After the environment is initialized, it can be activated. Once activated, it will automatically launch the next time the working directory (VS Code/Positron) and/or RStudio project is opened.
::activate() renv
Configure .Rprofile (optional)
By default, base R packages are installed in a system-level directory (e.g., under Program Files on Windows). On the otherhand, user-installed packages are typically stored in a user-specific location like AppData.
When a new renv environment is initialized, it sets up a local library within the project directory. During package installation, if a needed package already exists elsewhere on your system, renv will create a symbolic link (symlink) to that existing package to avoid duplicating packages across multiple projects. This behavior saves disk space and speeds up environment setup. However, in my workflow, I use OneDrive to access projects across multiple machines and this symlink-based approach can cause issues.
To avoid symlinks entirely and ensure all packages are physically stored within the project (mimicking Python’s venv behavior), I add the following line to a project’s .Rprofile file. This ensures that packages are stored in the within-project library as oposed to the user-specific default location.
options(renv.settings.use.cache = FALSE) # Set to false to prevent symlinks
Check settings and library paths
The following commands can be used to check library paths and settings to ensure the renv is properly configured.
::settings$use.cache()
renv::paths$library() # Check libraries renv
Configure .renvignore
Upon startup, renv scans the project files to detect which R functions are used, in order to identify missing packages and prompt installation of any missing dependencies. While useful, this scanning process can slow down loading the renv, especially if your project includes large or irrelevant files such as Word documents, PDFs, or Python libraries.
A .renvignore file can be configured to list any directories or file types that should be excluded from scanning. To create .renvignore in Positron, right-click in the Explorer pane and choose “New File…”, then name the file .renvignore. Next, open the saved file and add separate lines for directories or file types that should not be scanned.
/py_env # excludes the py_env directory
*.html # excludes all .html files
*.yml # excludes all .yml files
*.ipynb # excludes all .ipynb files
Install packages with renv
::install() renv
Check the status of a project and record the current state of packages
::status()
renv::snapshot() renv