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

Making odbr fail gracefully (Closes #12) #37

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions R/download.R

This file was deleted.

45 changes: 45 additions & 0 deletions R/download_piggyback.R
Original file line number Diff line number Diff line change
@@ -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(

Check warning on line 27 in R/download_piggyback.R

View workflow job for this annotation

GitHub Actions / lint

file=R/download_piggyback.R,line=27,col=8,[indentation_linter] Indentation should be 6 spaces but is 8 spaces.

Check warning on line 27 in R/download_piggyback.R

View workflow job for this annotation

GitHub Actions / lint-project

file=R/download_piggyback.R,line=27,col=8,[indentation_linter] Indentation should be 6 spaces but is 8 spaces.
file = filename_to_download,
repo = "hsvab/odbr",
dest = temp_dest_dir
)
)
}

# Halt function if download failed
if (!file.exists(temp_full_file_path) ) {

Check warning on line 36 in R/download_piggyback.R

View workflow job for this annotation

GitHub Actions / lint

file=R/download_piggyback.R,line=36,col=40,[spaces_inside_linter] Do not place spaces before parentheses.

Check warning on line 36 in R/download_piggyback.R

View workflow job for this annotation

GitHub Actions / lint-project

file=R/download_piggyback.R,line=36,col=40,[spaces_inside_linter] Do not place spaces before parentheses.
message('Internet connection not working properly.')

Check warning on line 37 in R/download_piggyback.R

View workflow job for this annotation

GitHub Actions / lint

file=R/download_piggyback.R,line=37,col=13,[quotes_linter] Only use double-quotes.

Check warning on line 37 in R/download_piggyback.R

View workflow job for this annotation

GitHub Actions / lint-project

file=R/download_piggyback.R,line=37,col=13,[quotes_linter] Only use double-quotes.
return(invisible(NULL))

} else {
# return string with the path to the file saved in a tempdir

Check warning on line 41 in R/download_piggyback.R

View workflow job for this annotation

GitHub Actions / lint

file=R/download_piggyback.R,line=41,col=2,[indentation_linter] Indentation should be 4 spaces but is 2 spaces.

Check warning on line 41 in R/download_piggyback.R

View workflow job for this annotation

GitHub Actions / lint-project

file=R/download_piggyback.R,line=41,col=2,[indentation_linter] Indentation should be 4 spaces but is 2 spaces.
return(temp_full_file_path)
}

}
3 changes: 3 additions & 0 deletions R/read_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
# 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) }

Check warning on line 86 in R/read_map.R

View workflow job for this annotation

GitHub Actions / lint

file=R/read_map.R,line=86,col=5,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.

Check warning on line 86 in R/read_map.R

View workflow job for this annotation

GitHub Actions / lint

file=R/read_map.R,line=86,col=35,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.

Check warning on line 86 in R/read_map.R

View workflow job for this annotation

GitHub Actions / lint

file=R/read_map.R,line=86,col=50,[brace_linter] Closing curly-braces should always be on their own line, unless they are followed by an else.

Check warning on line 86 in R/read_map.R

View workflow job for this annotation

GitHub Actions / lint-project

file=R/read_map.R,line=86,col=5,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.

Check warning on line 86 in R/read_map.R

View workflow job for this annotation

GitHub Actions / lint-project

file=R/read_map.R,line=86,col=35,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.

Check warning on line 86 in R/read_map.R

View workflow job for this annotation

GitHub Actions / lint-project

file=R/read_map.R,line=86,col=50,[brace_linter] Closing curly-braces should always be on their own line, unless they are followed by an else.

# Reading shape files
od_map <- sf::read_sf(temporary_filename)

Expand Down
3 changes: 3 additions & 0 deletions R/read_od.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
# 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) }

Check warning on line 55 in R/read_od.R

View workflow job for this annotation

GitHub Actions / lint

file=R/read_od.R,line=55,col=5,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.

Check warning on line 55 in R/read_od.R

View workflow job for this annotation

GitHub Actions / lint

file=R/read_od.R,line=55,col=35,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.

Check warning on line 55 in R/read_od.R

View workflow job for this annotation

GitHub Actions / lint

file=R/read_od.R,line=55,col=50,[brace_linter] Closing curly-braces should always be on their own line, unless they are followed by an else.

Check warning on line 55 in R/read_od.R

View workflow job for this annotation

GitHub Actions / lint-project

file=R/read_od.R,line=55,col=5,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.

Check warning on line 55 in R/read_od.R

View workflow job for this annotation

GitHub Actions / lint-project

file=R/read_od.R,line=55,col=35,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.

Check warning on line 55 in R/read_od.R

View workflow job for this annotation

GitHub Actions / lint-project

file=R/read_od.R,line=55,col=50,[brace_linter] Closing curly-braces should always be on their own line, unless they are followed by an else.

# Reading the file to a release in odbr repository
od_file <- data.table::fread(temporary_filename,
sep = ";"
Expand Down
22 changes: 22 additions & 0 deletions man/download_piggyback.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading