R/createOutputDirectoryStructure.R

Defines functions createOutputDirectoryStructure

# Function to create the output directory structure for a Pipeline object
# 
# More detailed explaination here
# 
# @param data.partition A \code{data.frame} generated by the \code{.partitionData} function
# @param outputdir The absolute path to the output directory
# @param model.index A \code{data.frame} generated by the \code{.indexModels} function
# @param force Forcibly overwrite the contents in the output directory [default:FALSE]
# @param iter Create a directory for a specific iteration of cross-validation
# 
# @return Function will execute silently if there were no errors or warnings
# 
# @examples
# \dontrun{
# createOutputDirectoryStructure(data.partition,outputdir,model.index,force=F)
# }
# @keywords internal

createOutputDirectoryStructure = function(data.partition,outputdir,model.index,force=FALSE,iter=NULL){
  
  # check if the output directory exists, if not, create it
  if(file.exists(file.path(outputdir))){
    if(!force){
      if(file.exists(file.path(paste0(outputdir,"/cv_loop_1")))){
        stop("It looks like this directory already contains results from a previous pipeline run. To overwrite these results, set 'force=TRUE' or change 'outputdir' to write to a different directory. Warning: setting force=T will remove all subdirectories in the output directory with the pattern 'cv_loop_X'where X is an integer")
      }  
    }
  } else {
    tryCatch({
      dir.create(file.path(outputdir),recursive=T)
    }, warning = function(war) {
      stop(paste0("Cannot create output directory '",outputdir,"'. Likely reason: Permission denied\n...exiting"))
    })
  }
  
  # remove any cross-validation directories in the output directory
  if(force){
    if(is.null(iter)){
      old.files <- dir(path=outputdir,pattern="cv_loop_")  
    } else {
      old.files <- dir(path=outputdir,pattern=paste0("cv_loop_",iter))
    }
    if(length(old.files)>0){
      old.files <- file.path(paste(outputdir,old.files,sep="/"))
      unlink(old.files,recursive=T,force=F)
    }
  }
  
  # create the sub-directories that will contain the results of the pipeline
  for(i in 1:length(data.partition)){
    dir.create(file.path(paste0(outputdir,"/cv_loop_",i)),showWarnings=F)
    for(j in 1:nrow(model.index)){
      dir.create(file.path(paste0(outputdir,"/cv_loop_",i,"/model_",j)),showWarnings=F)
    }
  }
  
}
jperezrogers/rabbit documentation built on Feb. 9, 2020, 4:59 p.m.