R/readthedata.R

Defines functions readthedata

Documented in readthedata

#' Read the Simpact output files.
#'
#' \code{readthedata} imports the .csv files generated by 
#' \code{\link[RSimpactCyan]{simpact.run}} in the RSimpactCyan package, and
#' combines them into one list object. This object is in a format that makes
#' subsequent analysis easier.
#' 
#' @param modeloutput The list object that is produced by
#'   \code{\link[RSimpactCyan]{simpact.run}}.
#' @return A list, containing dataframes for the output of the model run: 
#'   \describe{
#'     \item{ptable}{People in the simulation}
#'     \item{rtable}{Relationship information} 
#'     \item{etable}{Events} 
#'     \item{ttable}{HIV treatment episodes} 
#'     \item{itable}{Input parameters} 
#'     \item{ltable}{Optional book-keeping log} 
#'     \item{vltable}{HIV viral load information}
#'     \item{ftable}{Optional Facility log} 
#'  }
#'@examples
#'cfg <- list()
#'modeloutput <- RSimpactCyan::simpact.run(configParams = cfg, destDir = "temp")
#'datalist <- readthedata(modeloutput)
#'
#' @importFrom data.table fread
#' @importFrom data.table setDT
#' @importFrom data.table setnames
#' @importFrom readcsvcolumns read.csv.columns
#' @export

readthedata <- function(modeloutput){
  
  # Specifying file paths to the objects created by simpact.run()
  path <- as.character(modeloutput["outputfile"])
  outputID <- as.character(modeloutput["id"])
  DestDir <- sub(pattern = paste0(outputID, "output.txt"), 
                 replacement = "", 
                 x = path, 
                 fixed = T)
  personfile <- paste0(DestDir, outputID, "personlog.csv")
  relationfile <- paste0(DestDir, outputID, "relationlog.csv")
  eventfile <- paste0(DestDir, outputID, "eventlog.csv")
  treatmentfile <- paste0(DestDir, outputID, "treatmentlog.csv")
  inputparamfile <- paste0(DestDir, outputID, "settingslog.csv")
  periodicfile <- paste0(DestDir, outputID, "periodiclog.csv")
  viralloadfile <- paste0(DestDir, outputID, "hivviralloadlog.csv")
  facilitiesxyfile <- paste0(DestDir, outputID, "facilitypositions.csv")

  # Importing the person, viral load, relationship, and events
  # tables created by simpact.run()
  ptable <- data.table::fread(personfile, sep = ",", skip = 0)
  vltable <- data.table::fread(viralloadfile, sep = ",", skip = 0)
  rtable <- data.table::fread(relationfile, sep = ",", skip = 0)
  readetable <- readcsvcolumns::read.csv.columns(eventfile, 
                                                 has.header = FALSE,
                                                 column.types = "rssiirsiir")
  etable <- data.table::setDT(readetable)
  etable.colnames <- colnames(etable)
  
  # Naming the columns in the event log table
  if (ncol(etable) == 10){
    data.table::setnames(etable, etable.colnames,
             c("eventtime", "eventname", "p1name", "p1ID", "p1gender",
               "p1age", "p2name", "p2ID", "p2gender", "p2age"))
  } else {
    data.table::setnames(etable, etable.colnames,
             c("eventtime", "eventname", "p1name", "p1ID", "p1gender",
               "p1age", "p2name", "p2ID", "p2gender", "p2age", 
               "extradescript", "value"))

  }
  
  # Importing the treatment, and input parameter tables
  ttable <- data.table::fread(treatmentfile, sep  = ",", skip = 0)
  itable <- data.table::fread(inputparamfile, sep  = ",", skip = 0)

  # Putting together the list object, if the book-keeping and facility logs
  # are present in the simpact output
  if (file.exists(periodicfile) && file.exists(facilitiesxyfile)){
    
    # Importing the book-keeping log
    ltable <- data.table::fread(periodicfile, sep = ",", skip = 0)
    
    # Importing the facility log
    ftable <- data.table::fread(facilitiesxyfile, sep = ",", skip = 0)

    outputtables <- list(ptable = ptable, 
                         rtable = rtable, 
                         etable = etable,
                         ttable = ttable, 
                         itable = itable, 
                         ltable = ltable,
                         vltable = vltable, 
                         ftable = ftable)
    
  }else if(file.exists(facilitiesxyfile) && !file.exists(periodicfile)){
    
    # Import only the facility table
    ftable <- data.table::fread(facilitiesxyfile, sep = ",", skip = 0)

    outputtables <- list(ptable = ptable, 
                         rtable = rtable, 
                         etable = etable,
                         ttable = ttable, 
                         itable = itable, 
                         vltable = vltable, 
                         ftable = ftable)

  }else if(!file.exists(facilitiesxyfile) && file.exists(periodicfile)){
    
    # Import only the book-keeping log
    ltable <- data.table::fread(periodicfile, sep = ",", skip = 0)

    outputtables <- list(ptable = ptable, 
                         rtable = rtable, 
                         etable = etable,
                         ttable = ttable, 
                         itable = itable, 
                         ltable = ltable, 
                         vltable = vltable)
  }else{
    outputtables <- list(ptable = ptable, 
                         rtable = rtable,
                         etable = etable, 
                         ttable = ttable, 
                         itable = itable, 
                         vltable = vltable)
  }

  return(outputtables)
}
wdelva/RSimpactHelp documentation built on Dec. 26, 2019, 3:42 a.m.