R/NewProject.R

Defines functions proj_setup

proj_setup <- function(path, ...){
  # ensure path exists
  dir.create(path, recursive = TRUE, showWarnings = FALSE)
  dots <- list(...)
  attach(dots)
  on.exit(detach(dots))
  ProjectName <- paste0(path)

  ### Setup ReadMe Files ----
  readme <- c(paste0("# ", ProjectName, "  "),
              paste0("**PI:**", PI, "  "),
              paste0("**Analyst**:", analyst, "  "),
              "",
              "Details about the folders:",
              '',
              "File | Description",
              "---|----------------------------------------------------------",
              paste("Admin | contains the scope of work and other",
                    "administrative documents"),
              paste("Background | contains the background information for",
                    "the analysis"),
              "Code | contains all R scripts for this project",
              "DataRaw | contain all raw data provided by investigators",
              "DataProcessed | contains the processed data used for analysis",
              paste("Dissemination | contains any materials produced for",
                    "dissemination, ie. Abstracts, Posters, Papers"),
              "Reports | contains all output, rmarkdown files and report")


  # write to readme file
  writeLines(paste0(readme, collapse = '\n'),
             con = file.path(path, "ReadMe.md"))

  # readme files
  CreateReadMe(path = path)

  ### Create Meta File ----
  # for project info
  if(meta){
    dir.create(paste0(path, '/.ProjData'))
    ProjData <- list(ProjectName = ProjectName, PI = PI, analyst = analyst)
    write.dcf(ProjData, file.path(path, '/.ProjData/Data.dcf'))
  }

  #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
  ### Setup Git ----

  # add to current gitignore if exists
  if(file.exists(file.path(path, '.gitignore'))){
    gitignore <- readLines(con = file.path(path, '.gitignore'))
  } else {
    gitignore <- NULL
  }
  # add R template gitignore
  # (source: https://github.com/github/gitignore/blob/master/R.gitignore)
  gitignore <- paste0(c(gitignore,
                        "# History files",
                        ".Rhistory",
                        ".Rapp.history",

                        "# Session Data files",
                        ".RData",

                        "# User-specific files",
                        ".Ruserdata",

                        "# Example code in package build process",
                        "*-Ex.R",

                        "# Output files from R CMD build",
                        "/*.tar.gz",

                        "# Output files from R CMD check",
                        "/*.Rcheck/",

                        "# RStudio files",
                        ".Rproj.user/",

                        "# produced vignettes",
                        "vignettes/*.html",
                        "vignettes/*.pdf",

                        paste0("# OAuth2 token, see https://github.com/",
                               "hadley/httr/releases/tag/v0.3"),
                        ".httr-oauth",

                        "# knitr and R markdown default cache directories",
                        "/*_cache/",
                        "/cache/",

                        "# Temporary files created by R markdown",
                        "*.utf8.md",
                        "*.knit.md"), collapse = '\n')

  # by file type
  if(nodata == 'By File Type'){
    gitignore <- paste0(c(gitignore,
                          "# R Data files",
                          "*.RData",
                          "*.rda",
                          "*.rdata",
                          "*.rda",
                          "# Text files",
                          "*.csv",
                          "*.txt",
                          "*.dat",
                          "# Excel",
                          "*.xls*",
                          "# SAS",
                          "*.sas7bdat",
                          "*.xport",
                          "# Access",
                          "*.mdb"), collapse = '\n')
    }

    # by Folder
    if(nodata == "By Location"){
      gitignore <- paste0(c(gitignore,
                            "DataRaw/*",
                            "DataProcessed/*",
                            "!*/ReadMe.md"), collapse = '\n')

    }


    # git initialize
    if(git_init){
      if (!requireNamespace('git2r', quietly = T)) {
        warning('git2r is required for git initialization')
      } else{
        tryCatch({
        writeLines(gitignore, con = file.path(path, '.gitignore'))
        repo <- git2r::init(path)
        if(remote_origin != '') git2r::remote_add(repo, 'origin', remote_origin)
        if(initcommit) {
          git2r::add(repo, 'ReadMe.md')
          git2r::commit(repo, message = 'Initial Commit')
          if(remote_origin != '') {
          system(paste('cd', path, '&& git push -u origin master'))
          }
        }
        }, error = function(e){
          paste0('There was an error setting up the git repo',
                 e)
        })
      }
    }



}
ledbettc/CIDAtools documentation built on Jan. 24, 2020, 3:56 p.m.