How to use `renv`?

Hi team,

I'm wondering how to use renv from the command line? Using Ubuntu 24, the Posit supplied version of R 4.5.1, and renv 1.1.5.

I have a new, empty, directory, with full permissions in my HOME called renv-4.5.1. When in that directory I open R with /opt/R/4.5.1/bin/R -q, and have tried:

  • renv::init(); restart R; renv::install(x); renv::snapshot(x)
  • renv::init(); restart R; renv::install(x); library(x); renv::snapshot()
  • renv::init(); restart R; install.packages(x); renv::snapshot()
  • renv::init(); restart R; install.packages(x); library(x); renv::snapshot()

In each of the first two cases, I get packages installed into /path/renv/library/linux-ubuntu-noble/R-4.5/x86_64-pc-linux-gnu/

In the second two cases I get packages installed into my home-dir R package cache.

In all four cases, when I run renv::snapshot() the output I see is:

- The lockfile is already up to date.

When I quit out of R, and then check the renv.lock file, the only thing I see in it is the renv package. None of the other packages installed are listed.

From what I've read in the documentation, that is not how it is meant to work?

What am I doing wrong in my process? Thanks

Ok, I've just watched a yt clip on this, and I think renv requires there to be an R file - test.R for example - with the library commands in it to work?

By default renv::snapshot() scans existing R files to discover used packages, to snapshot current library state, try with

renv::snapshot(type = "all")
1 Like

Yes, it's looking for libraries to be loaded in your code. Just installing them doesn't add them to renv.

1 Like

I guess it's now obvious that I come from Python land where no files are required for adding packages to an env. Thanks everyone, appreciate your help working through this

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.