R/wsv.reader.R

Defines functions wsv.reader

Documented in wsv.reader

#' Read a whitespace separated values (.wsv or .txt) file.
#'
#' This function will load a data set stored in the WSV 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{wsv.reader('example.wsv', 'data/example.wsv', 'example')}
#' @importFrom utils read.table
#' @importFrom utils unzip
wsv.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.table(filename,
                  header = config$data_loading_header),
         envir = .TargetEnv)
}
connectedblue/ProjectTemplate2 documentation built on May 17, 2019, 2:46 p.m.