R/use_dir_structure.R

Defines functions use_dir_structure

#' @keywords internal
use_dir_structure <- function(project = ".", repos, targets) {
  proj <- normalizePath(project, mustWork = FALSE)

  callr::r(
    func = function(
      .project,
      targets,
      git_repository,
      repos,
      use_targets,
      use_rprofile
    ) {
      withr::with_dir(.project, {
        if (
          file.exists(file.path(.project, ".Rprofile")) &&
            grepl('source("renv/activate.R")', readLines(file.path(.project, ".Rprofile")))
        ) {
          message('".Rprofile" already exists! Nothing was done!')
          return(TRUE)
        }
        use_rprofile()

        renv::scaffold(repos = repos)
        renv::activate()

        # Targets
        if (targets) {
          message("Please install the following R packages within the project ...")
          message(paste(
            '  renv::install(packages = c("here", "targets",',
            '"gittargets", "visNetwork"), prompt = FALSE)'
          ))
          use_targets()
        }

        renv::snapshot(prompt = FALSE, type = "all")
      })
    },
    args = list(
      .project = proj,
      targets = targets,
      repos = repos,
      use_targets = use_targets,
      use_rprofile = use_rprofile
    ),
    repos = repos
  )

  invisible(TRUE)
}
umr1283/umr1283 documentation built on Nov. 27, 2022, 5:17 p.m.