Can one use renv::restore() call within a package (properly)?

Let's say I have a package, pkgA. Key to the functionality of this package is the ability to help its user generate a reproducible project using git and renv. To that end, I'd like to have a single function that calls within it renv::activate() and renv::restore() when starting to work on a project initiated by pkgA. Something akin to this

# Run this every time you start working a project initiated by `pkgA::wrkflow_init()`
wrkflow_state_set  <- function (project_path = .){
  
  # Check custom infrastructure is set up properly
  if (!is_valid_wrkflow(path = project_path)) 
    stop(cli::format_error("Project workflow not properly set! Use wrkflow_init"))
  setwd(project_path)
  
  # Restore renv
  renv::activate()
  renv::restore()
}

The problem:
pkgA has dependency on a number of other packages, for example dplyr.

  • Let's say I am a user of pkgA and have version 1.0.8 of dplyr on my machine, but renv.lock for the project I am working on was set at version 1.0.7 at the time of its initiation.
  • I start additional dev on this project by first calling pkgA::wrkflow_state_set(".").
  • That loads dplyr version 1.0.8 per namespace of pkgA and the latest version of dplyr on my machine. It then proceeds to renv::active() and renv::restore(). At this point, there is no error or warning, but in fact restore has not done any magic to switch from 1.0.8 to 1.0.7 dplyr
  • Then needing dplyr in the project, I attach it :library(dplyr).
  • At this point, I get the error
Error in value[[3L]](cond) : 
  Package ‘dplyr’ version 1.0.8 cannot be unloaded:
 Error in unloadNamespace(package) : namespace ‘dplyr’ is imported by ‘pkgA’,  so cannot be unloaded

The Alternative: I can always ask the user to

  1. run renv::restore() manually and then
  2. run pkgA::wrkflow_state_set.

That works, but the two function calls are always to be run together and in that order, so it'd be much nicer if they can be folded into a single function.

Question: Is that single function possible in some way or the less convenient alternative is the only way?

This topic was automatically closed after 45 days. 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.