Skip to content

Commit

Permalink
Merge pull request #18 from inbo/16-new-function-download-file-from-g…
Browse files Browse the repository at this point in the history
…oogledrive

16 new function download file from googledrive
  • Loading branch information
SanderDevisscher authored May 13, 2024
2 parents 022b066 + 5d3b8a1 commit 39f4951
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
Imports:
dplyr (>= 1.1.4),
googledrive (>= 2.1.1),
magrittr (>= 2.0.3),
rlang (>= 1.1.3)
rlang (>= 1.1.3),
svDialogs (>= 1.1.0),
utils (>= 4.3.2)
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

export(check)
export(colcompare)
export(download_gdrive_if_missing)
importFrom(magrittr,"%>%")
103 changes: 103 additions & 0 deletions R/download_gdrive_if_missing.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#' Download gdrive if missing
#'
#' @author Sander Devisscher
#'
#' @description This function downloads the specified file from google drive if the destination
#' file does not exist. If it does exist the user will be prompted to download
#' it again.
#'
#' @param gfileID character google file token
#' @param destfile character destination filename with extention
#' @param update_always optional boolean to trigger a download everytime the
#' function is run. default is FALSE.
#' @param email optional character specifying the users email used to access the
#' googledrive file.
#'
#' @details
#' Its best practice to provide the email in encrypted form. This can be easily
#' achieved by adding email as an item in a .renviron file or even beter by using
#' more robust encryption methods.
#'
#' @returns If the destination file was missing it is now downloaded from the
#' googledrive.
#'
#' @examples
#' \dontrun{
#' # download newest version of the team charter
#' download_gdrive_if_missing(gfileID = "1gtqcZojPnbLhEgpul3r9sy2zK3UyyCVG",
#' destfile = "../../Teamcharters/Teamcharter_FIS.pdf",
#' email = Sys.getenv("email"),
#' update_always = TRUE)
#' }
#' \dontrun{
#' # download newest DRG Agouti export
#' download_gdrive_if_missing(gfileID = "1FX8DDyREKMH1M3iW9ijWjVjO_tBH8PXi",
#' destfile = "../fis-projecten/Grofwild/Drongengoed/Input/Agouti/drongengoed_240502.zip",
#' email = Sys.getenv("email"),
#' update_always = TRUE)
#' }

#' @export

download_gdrive_if_missing <- function(gfileID,
destfile,
update_always = FALSE,
email){
# Authentication ####
## email uit system variables
if (check(email) == 0) {
email <- Sys.getenv("email")
print("extracting email from System variables")
}

## email dmv popup
if (email == "") {
email <- svDialogs::dlg_input("je email adres:")
email <- email$res
}

## Authenticate
googledrive::drive_auth(email)

# Check whether file exists locally ####
## Check whether destpath exists locally ####
destpath <- dirname(destfile)

if(!dir.exists(destpath)){
# destpath doesn't exist => create ?
q_create_dir <- utils::askYesNo(msg = paste0(destpath, " aanmaken?"))

if(q_create_dir == TRUE | update_always == TRUE){
dir.create(destpath,
recursive = TRUE)
}
}

if(update_always == FALSE){
# update only when file doesn't exist or when user asks for it
if(file.exists(destfile)){
# destfile bestaat => update ?
q_update_destfile <- utils::askYesNo(paste0("update ", gsub(pattern = destpath,
replacement = "",
destfile), "?"))
if(q_update_destfile == TRUE){
download <- TRUE
}else{
download <- FALSE
}
}else{
# destfile bestaat niet => download!
download <- TRUE
}
}else{
# update everytime
download <- TRUE
}

# Download ####
if(download == TRUE){
googledrive::drive_download(googledrive::as_id(gfileID),
path = destfile,
overwrite = TRUE)
}
}
46 changes: 46 additions & 0 deletions man/download_gdrive_if_missing.Rd

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

0 comments on commit 39f4951

Please sign in to comment.