R/read_rds_remote.R

Defines functions read_rds_remote

Documented in read_rds_remote

#' Function to read an \code{.rds} file from a \code{URL}. 
#' 
#' @param file_remote Remote file name. 
#' 
#' @param quiet Should the function give messages? 
#' 
#' @author Stuart K. Grange
#' 
#' @return An R data class. 
#' 
#' @export
read_rds_remote <- function(file_remote, quiet = TRUE) {
  
  # Remote read
  if (grepl("^http", file_remote, ignore.case = TRUE)) {
    
    # Make temp local copy
    file_local <- basename(file_remote)
    file_local <- file.path(tempdir(), file_local)
    
    # Download file
    download.file(file_remote, file_local, quiet = quiet, mode = "wb")
    
    # Load file
    df <- readRDS(file_local)
    
  } else {
    
    # Direct read
    df <- readRDS(file_remote)
    
  }
  
  # Return
  df
  
}
skgrange/threadr documentation built on May 11, 2024, 12:16 p.m.