R/batchOutFiles.R

#' Process Batch Out Files For Sortie
#'
#' This is a function to process batches of .out files generated by SORTIE-ND's
#' summary output. It loops through a list of filenames given and produces a
#' single data.frame as output. This function relies on \code{\link{getOutFile}}
#' and \code{\link{parseOutFile}}.
#'
#' @param namepattern A character string that matches the unique part of your
#'   batch files. E.g., if all files in a particular batch had "monkey" in them,
#'   this function would search any file in that directory with the string
#'   "monkey" inside of it.
#' @param sdir The path to the directory to search. Default is ".", representing
#'   your current working directory.
#' @param yearoffset The amount to subtract from the "step" variable to account
#'   for intial plot loading.
#' @return This function returns a data.frame of cleaned and merged files. Any
#'   personally identifying information will be lost (e.g., filename).
#'
#' @export

batchOutFiles <- function(namepattern="", sdir=".", yearoffset=0, numsubplots=1){

  ## type check
  if(!is.character(namepattern) | !is.character(sdir)){
    stop("namepattern and sdir must be character strings")
  }
  if(!is.numeric(yearoffset) | !is.numeric(numsubplots)){
    stop("yearoffset and numsubplots must be numeric")
  }


  listoffiles <- list.files(pattern=namepattern, path=sdir)
  listoffiles <- paste(sdir, listoffiles, sep="/")
  #print(listoffiles)
  ## error checking
  #if(listoffiles == paste(sdir, "/", sep="")){ stop("No files were found.")}
  responseDf <- data.frame()
  for(i in 1:length(listoffiles)){
    tempdat <- getOutFile(listoffiles[i], numsubplots)
    newdf <- parseOutFile(tempdat)
    responseDf <- rbind(responseDf, newdf)
  }

  responseDf$Step <- responseDf$Step - yearoffset
  responseDf <- responseDf[responseDf$Step > 0,]
  return(reNumber(responseDf))
}
ecology-rocks/SortieIO documentation built on May 15, 2019, 7:57 p.m.