R/csv.reader.R

Defines functions csv.reader

Documented in csv.reader

#' Read a comma separated values (.csv) file.
#'
#' This function will load a data set stored in the CSV file format into
#' the specified global variable binding.
#'
#' @param data.file The name of the data file to be read.
#' @param filename The path to the data set to be loaded.
#' @param variable.name The name to be assigned to in the global environment.
#'
#' @return No value is returned; this function is called for its side effects.
#'
#' @examples
#' library('ProjectTemplate2')
#'
#' \dontrun{csv.reader('example.csv', 'data/example.csv', 'example')}
#' @importFrom utils read.csv
#' @importFrom utils unzip
csv.reader <- function(data.file, filename, variable.name)
{
  if (grepl('\\.zip$', filename))
  {
    tmp.dir <- tempdir()
    tmp.path <- file.path(tmp.dir, data.file)
    file.copy(filename, tmp.path)
    unzip(filename, exdir = tmp.dir)
    filename <- file.path(tmp.dir, sub('\\.zip$', '', data.file))
  }

  assign(variable.name,
         read.csv(filename,
                  header = config$data_loading_header,
                  sep = ','),
         envir = .TargetEnv)
}
connectedblue/ProjectTemplate2 documentation built on May 17, 2019, 2:46 p.m.