Use renv for reproducible R deployments
This guide walks through using renv to ensure specific R package versions are deployed to Posit Connect. This is useful when you need to pin package versions or when collaborating on projects where reproducibility matters.
Prerequisites
Before you begin, you must have:
Background
When you deploy R content to Posit Connect, the deployment tools capture your package dependencies and record them in a manifest.json file. Connect then recreates your package environment on the server. For more details on how this process works, see Understanding packages during deployment.
By default, package versions are determined by inspecting your active R library. Using renv gives you explicit control over which versions are recorded and deployed.
Step 1: Initialize renv in your project
From your project directory, initialize renv:
renv::init()This creates:
- An
renv.lockfile that records all package versions - A project-local library where packages are installed
- An
.Rprofilethat activates renv when you open the project
Step 2: Install the package versions you need
Install packages at specific versions using renv::install(). For example, to install a specific version of a package:
renv::install("jsonlite@2.0.0")After installing packages, update the lockfile to record them:
renv::snapshot()The renv.lock file now contains the exact versions of all packages your project uses.
Step 3: Generate the manifest
As of rsconnect 1.0.0, when an renv.lock file exists in your deployment directory, the package versions and sources defined in that file are automatically used when generating the manifest.json:
rsconnect::writeManifest()You can verify the manifest contains your expected package versions by examining the generated manifest.json file.
Step 4: Deploy your content
Deploy your content using your preferred method:
rsconnect::deployApp()Connect uses the package information from the manifest (which came from your renv.lock) to reconstruct the environment with the exact versions you specified.
Keeping renv.lock and manifest.json in sync
When collaborating on a project, ensure that whenever renv.lock is updated, the manifest.json is regenerated. This prevents situations where Connect deploys with different package versions than intended.
A simple workflow:
- Make changes to your R environment (install or update packages).
- Run
renv::snapshot()to updaterenv.lock. - Run
rsconnect::writeManifest()to regenerate the manifest. - Commit both files to version control.
When a collaborator pulls your changes, they can run renv::restore() to install the exact package versions from the lockfile into their local environment.
Troubleshooting
See the renv frequently asked questions and using renv with Posit Connect for additional help. ### Packages install from an unexpected location
A Connect administrator can override the R package repositories that appear in the manifest. See R package repositories in the Admin Guide for details on how repository resolution works.
A package version is not available
Ensure that the repository serving your packages has the version you need. If you use Posit Package Manager, check that the snapshot date includes the version you need.