From 445110a8d53ace41bc770194a85d2a9d549c7a7c Mon Sep 17 00:00:00 2001 From: rafapereirabr Date: Fri, 3 Nov 2023 20:07:26 -0300 Subject: [PATCH 1/3] documented version of function download_piggyback --- R/download.R | 23 ----------------------- R/download_piggyback.R | 34 ++++++++++++++++++++++++++++++++++ man/download_piggyback.Rd | 22 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 23 deletions(-) delete mode 100644 R/download.R create mode 100644 R/download_piggyback.R create mode 100644 man/download_piggyback.Rd diff --git a/R/download.R b/R/download.R deleted file mode 100644 index 55e4886..0000000 --- a/R/download.R +++ /dev/null @@ -1,23 +0,0 @@ -# Requiring as parameter the filename_to_download it returns the R object "temporary_filename" -download_piggyback <- function(filename_to_download, force_download = FALSE) { - # Defining our temporary directory - temp_dest_dir <- tempdir(check = TRUE) - - # Creating the temporary folder effectively - fs::dir_create(path = temp_dest_dir, recurse = TRUE) - - # Creating path + filename and saving to "temporary_filename" - temp_full_file_path <- paste0(temp_dest_dir, "/", filename_to_download) - - if (!file.exists(temp_full_file_path) || force_download) { - # Uploading the file to a release of the odbr repo - release specified in the parameter - piggyback::pb_download( - file = filename_to_download, - repo = "hsvab/odbr", - dest = temp_dest_dir - ) - } - - # Uploading the file to a release of the odbr repo - release specified in the parameter - return(temp_full_file_path) -} diff --git a/R/download_piggyback.R b/R/download_piggyback.R new file mode 100644 index 0000000..f430534 --- /dev/null +++ b/R/download_piggyback.R @@ -0,0 +1,34 @@ +#' Download file from odbr using piggyback +#' +#' @param filename_to_download String. The name of the file to be downloaded. +#' @param force_download Logical. If `FALSE` (default) the function will read +#' cached data downloaded previously in the same session. If `TRUE`, the +#' function will download the data and overwrite cached data. +#' +#' @return A string with the path to the file saved in a tempdir +#' +#' @keywords internal +download_piggyback <- function(filename_to_download, + force_download = FALSE) { + + # Defining our temporary directory + temp_dest_dir <- tempdir(check = TRUE) + + # Creating the temporary folder effectively + fs::dir_create(path = temp_dest_dir, recurse = TRUE) + + # Creating path + filename and saving to "temporary_filename" + temp_full_file_path <- paste0(temp_dest_dir, "/", filename_to_download) + + if (!file.exists(temp_full_file_path) || force_download) { + # downloading the file from a release of the odbr repo - release specified in the parameter + piggyback::pb_download( + file = filename_to_download, + repo = "hsvab/odbr", + dest = temp_dest_dir + ) + } + + # return string with the path to the file saved in a tempdir + return(temp_full_file_path) +} diff --git a/man/download_piggyback.Rd b/man/download_piggyback.Rd new file mode 100644 index 0000000..351eed6 --- /dev/null +++ b/man/download_piggyback.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/download_piggyback.R +\name{download_piggyback} +\alias{download_piggyback} +\title{Download file from odbr using piggyback} +\usage{ +download_piggyback(filename_to_download, force_download = FALSE) +} +\arguments{ +\item{filename_to_download}{String. The name of the file to be downloaded.} + +\item{force_download}{Logical. If \code{FALSE} (default) the function will read +cached data downloaded previously in the same session. If \code{TRUE}, the +function will download the data and overwrite cached data.} +} +\value{ +A string with the path to the file saved in a tempdir +} +\description{ +Download file from odbr using piggyback +} +\keyword{internal} From 400d98f7f492d70bce1e9cb6169e1878ebcff960 Mon Sep 17 00:00:00 2001 From: rafapereirabr Date: Fri, 3 Nov 2023 20:12:08 -0300 Subject: [PATCH 2/3] Making read_od fail gracefully --- R/download_piggyback.R | 21 ++++++++++++++++----- R/read_od.R | 3 +++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/R/download_piggyback.R b/R/download_piggyback.R index f430534..1b47010 100644 --- a/R/download_piggyback.R +++ b/R/download_piggyback.R @@ -21,14 +21,25 @@ download_piggyback <- function(filename_to_download, temp_full_file_path <- paste0(temp_dest_dir, "/", filename_to_download) if (!file.exists(temp_full_file_path) || force_download) { + # downloading the file from a release of the odbr repo - release specified in the parameter - piggyback::pb_download( - file = filename_to_download, - repo = "hsvab/odbr", - dest = temp_dest_dir + try(silent = TRUE, + piggyback::pb_download( + file = filename_to_download, + repo = "hsvab/odbr", + dest = temp_dest_dir + ) ) } + # Halt function if download failed + if (!file.exists(temp_full_file_path) ) { + message('Internet connection not working properly.') + return(invisible(NULL)) + + } else { # return string with the path to the file saved in a tempdir - return(temp_full_file_path) + return(temp_full_file_path) + } + } diff --git a/R/read_od.R b/R/read_od.R index b1d3d05..d16143e 100644 --- a/R/read_od.R +++ b/R/read_od.R @@ -51,6 +51,9 @@ read_od <- function(city = "S\u00E3o Paulo", # parameter and saving the return in "temporary_filename" temporary_filename <- download_piggyback(filename_to_download, force_download) + # check if download worked + if(is.null(temporary_filename)) { return(NULL) } + # Reading the file to a release in odbr repository od_file <- data.table::fread(temporary_filename, sep = ";" From e4f643a66647254728a40e7609f0005742382dba Mon Sep 17 00:00:00 2001 From: rafapereirabr Date: Fri, 3 Nov 2023 20:16:20 -0300 Subject: [PATCH 3/3] Make read_map fail gracefully --- R/read_map.R | 3 +++ 1 file changed, 3 insertions(+) diff --git a/R/read_map.R b/R/read_map.R index f2771e4..e2fdde9 100644 --- a/R/read_map.R +++ b/R/read_map.R @@ -82,6 +82,9 @@ read_map <- function(city = "S\u00E3o Paulo", # parameter and saving the function return in "temporary_filename" temporary_filename <- download_piggyback(filename_to_download, force_download) + # check if download worked + if(is.null(temporary_filename)) { return(NULL) } + # Reading shape files od_map <- sf::read_sf(temporary_filename)