R/load_stash.R

#' Get/Load a stashed object
#'
#' @param file.name Base file name for the object
#' @param from Path for where the object resides. Accepts a character
#'    string or *_stash object.
#' @param time.stamp TRUE or FALSE. When TRUE, files containing a time stamp
#'    generated by save_stash, or matching the save_stash pattern, will be
#'    loaded.
#' @param uuid TRUE or FALSE. When TRUE, files containing a uuid
#'    generated by save_stash, or matching the save_stash pattern, will be
#'    loaded.
#' @param extension Extension for the file to load.
#' @param compression Accepts NULL for no compression, or 'gz' for gzip.
#' @param checksum Not being used right now.
#' @param read.fn Any function where the first argument is the object and the
#'    second is the file path. Will be used to read the files into R.
#' @param ... Arguments to pass to \code{read.fn}.
#'
#' @return List of stash objects that were successfully saved.
#' @export
load_stash <- function(from, read.fn = read.csv, ...) {

  ## TODO: Incorporate mem_stash
  from <- as.stash_file(from, simplify = FALSE)
  from <- from[file_exists(from)]

  if (length(from) == 0) {
    message('No files found to load.')
    return(NULL)
  }

  ## TODO: Enhance Prioritization of list items
  from <- from[[1]]
  load_stash_(from, read.fn, ...)
}


load_stash_ <- function(from, read.fn, ...) {
  UseMethod('load_stash_')
}



#' @export
load_stash_.local_stash <- function(from, read.fn, ...) {
  message('Reading... ', from)
  read.args <- list(quote(from), ...)
  read.res <- do.call(read.fn, read.args)
  return(read.res)
}


#' @export
load_stash_.s3_stash <- function(from, read.fn, ...) {

  ## Need to pull the files locally
  message('Moving temp file locally...')
  ## Need to save the object to a temp file in order to load from s3
  temp.stash <- as.local_stash(from, path = tempfile())
  temp.file <- suppressMessages(
    copy_stash(from = from, to = temp.stash, keep.from = TRUE))

  message('Reading from temp file...')
  read.args <- list(quote(temp.file), ...)
  read.res <- do.call(read.fn, read.args)

  ## Remove the temp files
  unlink(temp.stash, recursive = TRUE)

  return(read.res)
}
jason-huling/rstash documentation built on May 18, 2019, 4:53 p.m.