How can I add git support for a custom RStudio Project Template?

Hi,

I am interested in adding an RStudio Project Template to the R package I am working on. This is pretty straightforward to achieve by adding a DCF file in pkg/inst/rstudio/templates/project and binding to an R function that does the work of setting up the project template. A simple example:

# project.dcf
Title: Project Template Name
Subtitle: Some great description...
Binding: project_fun
project_fun <- function(path, ...) {
  dir.create(path)
}

However, I am interested in also having a checkbox for initiating a git repo - similar to the option available when clicking File > New Project... > New Directory > New Project. I thought the following adjustments would work...

# project.dcf
Title: Project Template Name
Subtitle: Some great description...
Binding: project_fun

Parameter: git
Widget: CheckboxInput
Label: Create a git repository
project_fun <- function(path, ...) {
  dots <- list(...)
  dir.create(path)
  if (dots$git) usethis::use_git()
}

But this produces the error below because usethis::use_git() cannot be called outside a package or project directory, and it seems like the .Rproj file isn't added to path directory until after project_fun() has finished its execution.

Error: Path '/Users/Matt/Desktop/test/' does not appear to be inside a project or package.

Anyone have any suggestions for how to support initializing a git repo with a custom RStudio Project Template, similar to creating a standard New Project in RStudio?

1 Like

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