How can I install packages to a new renv in an RProject template?

14 Views Asked by At

I'd like to write an RProject template for some analyses I do regularly. I'm following some guides here. Because I typically use renv in these analyses, I'd like to add an option to use renv upon setup. However, when I do setup projects with my template, renv doesn't appear to work.

Here is my setup function

statistical_analysis_template <- function(path, ...) {

  dots <- list(...)
  # ensure path exists
  dir.create(path, recursive = TRUE, showWarnings = FALSE)

  if(dots$use_renv) {

    # If using renv, there are some packages I will very likely need
    packages <- c('dplyr')

    renv::init(project=path, bare=T, force=F, load = F, restart = F)
    renv::install(packages = packages, project=path, prompt = FALSE)
  }

}

and here is my dcf file

Binding: statistical_analysis_template
Title: Statistical Analysis Template

Parameter: use_renv
Widget: CheckboxInput
Label: Use Renv
Default: On
Position: left

When I load my library and use the template from the GUI, I can successfully create a new project with the template. I can see the renv/ directory and lock file in the project directory. I can activate the renv using renv::activate.

But dplyr is not installed. When I search for documentation for dplyr::select I am returned

 ?dplyr::select
Error in find.package(if (is.null(package)) loadedNamespaces() else package,  : 
  there is no package called ‘dplyr’
0

There are 0 best solutions below