R/read_csv_gz_remote.R

Defines functions read_csv_gz_remote

read_csv_gz_remote <- function(url, col_types = NULL, verbose = TRUE) {
  
  # Get remote content with httr
  response <- httr::GET(url)
  
  # Check for errors, could also use httr::status_code
  # Only raise a message here too
  if (httr::http_error(response)) {
    if (verbose) {
      message("Remote resource is not available or location has changed...")
    }
    return(tibble())
  }
  
  # Create the connection
  con <- response %>% 
    httr::content() %>% 
    rawConnection() %>% 
    gzcon() 
  
  # Read data from the connection
  df <- readr::read_csv(con, col_types = col_types, progress = FALSE)
  
  # Ensure the connection is always closed
  close(con)

  return(df)
  
}

Try the saqgetr package in your browser

Any scripts or data that you put into this service are public.

saqgetr documentation built on Jan. 15, 2021, 2:57 p.m.