rstudio / renv

renv: Project environments for R.

Home Page:https://rstudio.github.io/renv/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use a package within .Rprofile ?

homer3018 opened this issue · comments

Hello,
I'm using the box package in most of my projects along with testthatand covr packages in CI/CD workflows. To make these work together, it's very convenient to have absolute file paths when using box::use, this is easily done with the help of here::here()and this can be set with an option.

As result my .Rprofile looks like this :

source("renv/activate.R")
options(box.path = here::here())

here is being listed by renv::dependencies() and hence is in my lock file.

My issue is when I try to renv::restore(clean = TRUE) in a workflow. My basic understanding (still onboarding with renv...) is that it looks like it's not yet restored the library and reads the .Rprofile file first, hence not finding the package, resulting in this error :

# Bootstrapping renv 1.0.3 ---------------------------------------------------
- Downloading renv ... OK
- Installing renv  ... OK

- One or more packages recorded in the lockfile are not installed.
- Use `renv::status()` for more details.
Error: Error in loadNamespace(x) : there is no package called ‘here’
Calls: options ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted
Error: Process completed with exit code 1.

If I remove the options line from my .Rprofile file, it restores successfully.

Is there a trick of some sort to make this work ?
One obvious alternative solution would be to not set the option but instead do something like
box::use(here::here("folder", "file")) every time I use box::use ? I'm of course not fond of this.

My basic understanding (still onboarding with renv...) is that it looks like it's not yet restored the library and reads the .Rprofile file first, hence not finding the package, resulting in this error

Just to confirm, your understanding is correct -- the renv autoloader sees that the renv package isn't installed, and so it tries to automatically bootstrap an installation of renv for you. Then, the rest of the .Rprofile is processed -- at this point, only the renv package is available.

You could add an extra piece here like:

if (!requireNamespace("here", quietly = TRUE)) {
  renv::install("here")
}

Or even renv::restore(packages = "here") if you know the here package will be available in the lockfile.

As an aside, why wouldn't it suffice to use options(box.path = getwd())? Or do you have a different kind of project structure in this case?

Thanks for the quick reply !
renv::restore(packages = "here") just before is working great. Also you're right about just using getwd(). I'm so used to here that it looks like I'm forgetting the basics :)