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..1b47010 --- /dev/null +++ b/R/download_piggyback.R @@ -0,0 +1,45 @@ +#' 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 + 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) + } + +} 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) 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 = ";" 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}