R/add_make_all.R

Defines functions add_make_all

Documented in add_make_all

#' Add MAKE ALL script
#'
#' Adds 99_make_clean.R to R/, and defines files
#' to be autogenerated. Currently defaults running
#' all prefix-numbered files up to 89 (files in the
#' 90's are assumed to be infrastructure, not analyses).
#'
#' @return adds 99_make_all.R to R/ if this doesn't
#'   already exist.
#' @import here
#' @export
add_make_all <- function(){
  
  if(file.exists(here("R", "99_make_all.R"))){
    cat("R/99_make_all.R already exists!\n")
  }else{
    file.create(here("R", "99_make_all.R"))
    files_in_r <- dir(here("R"))
    numbered_files <- 
      files_in_r[grep("^[[:digit:]]{2}_", files_in_r)]
    files_in_r_to_run <- 
      numbered_files[grep("[0-8]", 
                          substr(numbered_files, 1, 1))]

    file_string <- "files_in_r_to_run <- \n  c("
    if(length(files_in_r_to_run) > 0){
      for(i1 in 1:length(files_in_r_to_run)){
        file_string <-
          paste0(file_string, "\"",
                 files_in_r_to_run[i1], "\"")
        if(i1 < length(files_in_r_to_run)){
          file_string <- paste0(file_string, ",\n    ")
        }
      }
    }
    file_string <- paste0(file_string, ")")
        
    writeLines(
      c("library(here)",
        "library(rmarkdown)",
        "",
        "if(!dir.exists(here(\"results\"))){",
        "  dir.create(here(\"results\"))",
        "}",
        "",
        file_string,
        "",
        "if(length(files_in_r_to_run) > 0){",
        "",
        "  for(i1 in 1:length(files_in_r_to_run)){",
        "    rmarkdown::render(here(\"R\", files_in_r_to_run[i1]),",
        "      output_format =", 
        "        github_document(html_preview = TRUE,", 
        "          toc = TRUE),",
        "      output_dir = here(\"results\"))",
        "",
        "  }",
        "}",
        "",
        "if(file.exists(here(\"README.Rmd\"))){",
        "  rmarkdown::render(here(\"README.Rmd\"),",
        "    output_format =", 
        "      github_document(html_preview = TRUE,", 
        "        toc = TRUE),",
        "    output_dir = here())",
        "}"),
      
      con = here("R", "99_make_all.R")
    )
  }
  
}
kabagg/enar2018pkg documentation built on May 17, 2019, 7:29 p.m.