R/writeScenarios.R

Defines functions writeScenarios

Documented in writeScenarios

#' @title Write Scenarios
#' @description Write scenario data and setup folders for writing 
#'   genotype files from replicates
#'   
#' @param label label of simulation run.
#' @param scenarios data frame of scenario specifications generated by 
#'   \code{\link{makeScenarios}}.
#' @param google.drive.id id of root Google Drive to copy data to.
#' 
#' @return list of output folders.
#'   
#' @author Eric Archer \email{eric.archer@@noaa.gov}
#' 
#' @export
#' 
writeScenarios <- function(label, scenarios, google.drive.id = NULL) {
  fname <- scenariosFname(label)
  out.folder <- repFolder(label)
  google.rep.dir <- NULL
  if(!is.null(google.drive.id)) {
    label.pattern = paste0(fname, "|", out.folder)
    id <- googledrive::as_id(google.drive.id)
    drive.files <- googledrive::drive_ls(
      id, 
      pattern = label.pattern, 
      verbose = FALSE
    )
    if(nrow(drive.files) > 0) {
      stop(
        "The scenario file and/or replicate folder for '", label, 
        "' already exists on this Google Drive. ",
        "Either delete them or change the label for the run."
      )
    } else {
      google.rep.dir <- googledrive::drive_mkdir(
        out.folder, 
        parent = id, 
        verbose = FALSE
      )
    }
  }
  
  if(!dir.exists(out.folder)) dir.create(out.folder)
  utils::write.csv(scenarios, file = fname, row.names = FALSE)
  if(!is.null(google.drive.id)) {
    googledrive::drive_upload(
      fname, 
      path = googledrive::as_id(google.drive.id), 
      verbose = FALSE
    )
  }
  
  list(out = out.folder, google = google.rep.dir)
}
EricArcher/ebvSim documentation built on April 9, 2020, 3:39 p.m.