Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

download_read_table() #82

Open
eroten opened this issue Oct 3, 2024 · 0 comments
Open

download_read_table() #82

eroten opened this issue Oct 3, 2024 · 0 comments

Comments

@eroten
Copy link
Collaborator

eroten commented Oct 3, 2024

Originally used in ghg-cprg, a function that downloads a table from a remote URL, saves it in a given location, and reads it into your environment in one go is very handy.

#' Download and read in an Excel file or CSV from a remote URL
#'
#' @param url character, file location URL. Must end with either ".xlsx" or ".xls"
#' @param exdir character, directory location where to save downloaded document
#' @param force_download logical, whether to force a fresh download, regardless 
#'   of whether the file exists already. Default value is `FALSE`.
#' @param ... Additional arguments passed to readxl::read_excel() or readr::read_csv
#'
#' @return tibble
#'
#' @examples
#'
#' download_read_table("https://www.dot.state.mn.us/traffic/data/reports/Current_CC_StationList.xlsx",
#' "_transportation/data-raw/mndot/",
#' sheet = 1)
#'
#'
download_read_table <- function(url,
                                exdir,
                                force_download = FALSE,
                                ...) {
  # split URL to get file name
  url_split <- strsplit(url, split = "/")
  file_name <- tail(url_split[[1]], n = 1)
  
  # if the downloaded file does not already OR
  # we are forcing a fresh download
  # download the file and save in exdir
  if(!file.exists(file.path(exdir, file_name)) | force_download == TRUE){
    download.file(url,
                  destfile = file.path(exdir, file_name),
                  mode = "wb"
    )
  }
  
  # read and return file
  if (fs::path_ext(file_name) == "csv") {
    readr::read_csv(
      file = file.path(exdir, file_name),
      ...
    )
  } else {
    readxl::read_excel(path = file.path(exdir, file_name), ...)
  }
}
eroten added a commit to Metropolitan-Council/ghg-cprg that referenced this issue Oct 3, 2024
doesn't re-download by default if the file already exists! Referenced in Metropolitan-Council/councilR#82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant