R/create_enr_project.R

Defines functions create_enr_project create_project_existing create_enr_project_new list_files_and_dirs is_dir

Documented in create_enr_project

#' Create a new ENR project.
#'
#' This function will create all of the scaffolding for a new ENR project.
#' It will set up all of the relevant directories and their initial
#' contents. 
#'
#' @param project_name A character vector containing the name for this new
#'   project. Must be a valid directory name for your file system.
#'
#' @export
#'
#' @examples
#'
#' \dontrun{create_enr_project('new_enr_project')}
#' 

create_enr_project <- function(project_name = 'enr_project'){

  if (is_dir(project_name)) {
    create_project_existing(project_name)
  } else
    create_project_new(project_name)
  
  invisible(NULL)
}

create_project_existing <- function(project_name) {
  template_path <- system.file('project_skeleton/', package = 'ENRanalytics')
  template_files <- list_files_and_dirs(path = template_path)
  
  project_path <- file.path(project_name)
  
  file.copy(file.path(template_path, template_files),
            project_path,
            recursive = TRUE, overwrite = FALSE)
  
}

create_enr_project_new <- function(project_name) {
  if (file.exists(project_name)) {
    stop(paste("Cannot run create_enr_project() from a directory containing", project_name))
  }
  
  dir.create(project_name)
  tryCatch(
    create_project_existing(project_name = project_name),
    error = function(e) {
      unlink(project_name, recursive = TRUE)
      stop(e)
    }
  )
}

list_files_and_dirs <- function(path) {
  files <- list.files(path = path, all.files = TRUE, include.dirs = TRUE)
  files <- grep("^[.][.]?$", files, value = TRUE, invert = TRUE)
  files
}

is_dir <- function(path) {
  file.exists(path) && file.info(path)$isdir
}
AMUFacultyOfEnglish/ENRanalytics documentation built on May 5, 2019, 11:36 a.m.