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 version1.0.8
ofdplyr
on my machine, butrenv.lock
for the project I am working on was set at version1.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
version1.0.8
per namespace ofpkgA
and the latest version ofdplyr
on my machine. It then proceeds torenv::active()
andrenv::restore()
. At this point, there is no error or warning, but in fact restore has not done any magic to switch from1.0.8
to1.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
- run
renv::restore()
manually and then - 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?