From 8133a1179fa10be248b62206c7d8d0900d1daf4f Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Wed, 8 May 2024 12:23:49 +0200 Subject: [PATCH 1/9] Create download_gdrive_if_missing.R #16 --- R/download_gdrive_if_missing.R | 91 ++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 R/download_gdrive_if_missing.R diff --git a/R/download_gdrive_if_missing.R b/R/download_gdrive_if_missing.R new file mode 100644 index 0000000..56b6289 --- /dev/null +++ b/R/download_gdrive_if_missing.R @@ -0,0 +1,91 @@ +#' 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 +#' # example code +#' WIP +#' +#' @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 <- 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 <- 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(as_id(gfileID), + path = destfile, + overwrite = TRUE) + } +} From bbbd81d6db6532a050041b7d4779f53435df2406 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Wed, 8 May 2024 12:48:21 +0200 Subject: [PATCH 2/9] add example #16 --- R/download_gdrive_if_missing.R | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/R/download_gdrive_if_missing.R b/R/download_gdrive_if_missing.R index 56b6289..75a517c 100644 --- a/R/download_gdrive_if_missing.R +++ b/R/download_gdrive_if_missing.R @@ -22,8 +22,13 @@ #' googledrive. #' #' @examples -#' # example code -#' WIP +#' #' \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) +#' } #' #' @export @@ -33,13 +38,13 @@ download_gdrive_if_missing <- function(gfileID, email){ # Authentication #### ## email uit system variables - if(check(email) == 0){ + if (check(email) == 0) { email <- Sys.getenv("email") print("extracting email from System variables") } ## email dmv popup - if(email == ""){ + if (email == "") { email <- svDialogs::dlg_input("je email adres:") email <- email$res } @@ -84,7 +89,7 @@ download_gdrive_if_missing <- function(gfileID, # Download #### if(download == TRUE){ - googledrive::drive_download(as_id(gfileID), + googledrive::drive_download(googledrive::as_id(gfileID), path = destfile, overwrite = TRUE) } From 7d53c68f1c2d4edffd8128a5dd746a5220aeb703 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Wed, 8 May 2024 12:50:23 +0200 Subject: [PATCH 3/9] Update DESCRIPTION #16 --- DESCRIPTION | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7b62f7e..d87083f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -12,5 +12,7 @@ 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) From 4b65031952b520f89c175ce2bffc992d8f7863e8 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Wed, 8 May 2024 12:50:51 +0200 Subject: [PATCH 4/9] Create download_gdrive_if_missing.Rd #16 --- man/download_gdrive_if_missing.Rd | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 man/download_gdrive_if_missing.Rd diff --git a/man/download_gdrive_if_missing.Rd b/man/download_gdrive_if_missing.Rd new file mode 100644 index 0000000..1e0ca1e --- /dev/null +++ b/man/download_gdrive_if_missing.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/download_gdrive_if_missing.R +\name{download_gdrive_if_missing} +\alias{download_gdrive_if_missing} +\title{Download gdrive if missing} +\usage{ +download_gdrive_if_missing(gfileID, destfile, update_always = FALSE, email) +} +\arguments{ +\item{gfileID}{character google file token} + +\item{destfile}{character destination filename with extention} + +\item{update_always}{optional boolean to trigger a download everytime the +function is run. default is FALSE.} + +\item{email}{optional character specifying the users email used to access the +googledrive file.} +} +\value{ +If the destination file was missing it is now downloaded from the +googledrive. +} +\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. +} +\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. +} +\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) +} + +} +\author{ +Sander Devisscher +} From 1da09837a9f6f05e986ef8123f7d2cf1a8e83710 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Wed, 8 May 2024 12:50:57 +0200 Subject: [PATCH 5/9] Update NAMESPACE #16 --- NAMESPACE | 1 + 1 file changed, 1 insertion(+) diff --git a/NAMESPACE b/NAMESPACE index b243bc3..ba904a3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,4 +2,5 @@ export(check) export(colcompare) +export(download_gdrive_if_missing) importFrom(magrittr,"%>%") From a22cb8260ed2d62b9bd644828a827af172ab4739 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Wed, 8 May 2024 12:52:35 +0200 Subject: [PATCH 6/9] add utils:: #16 --- R/download_gdrive_if_missing.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/download_gdrive_if_missing.R b/R/download_gdrive_if_missing.R index 75a517c..4c64d33 100644 --- a/R/download_gdrive_if_missing.R +++ b/R/download_gdrive_if_missing.R @@ -58,7 +58,7 @@ download_gdrive_if_missing <- function(gfileID, if(!dir.exists(destpath)){ # destpath doesn't exist => create ? - q_create_dir <- askYesNo(msg = paste0(destpath, " aanmaken?")) + q_create_dir <- utils::askYesNo(msg = paste0(destpath, " aanmaken?")) if(q_create_dir == TRUE | update_always == TRUE){ dir.create(destpath, @@ -70,7 +70,7 @@ download_gdrive_if_missing <- function(gfileID, # update only when file doesn't exist or when user asks for it if(file.exists(destfile)){ # destfile bestaat => update ? - q_update_destfile <- askYesNo(paste0("update ", gsub(pattern = destpath, + q_update_destfile <- utils::askYesNo(paste0("update ", gsub(pattern = destpath, replacement = "", destfile), "?")) if(q_update_destfile == TRUE){ From c4246f23212c1f5704da6d1ac318e774bfe3f9ad Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Wed, 8 May 2024 12:52:44 +0200 Subject: [PATCH 7/9] add utils #16 --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index d87083f..2b3b12a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,4 +15,5 @@ Imports: googledrive (>= 2.1.1), magrittr (>= 2.0.3), rlang (>= 1.1.3), - svDialogs (>= 1.1.0) + svDialogs (>= 1.1.0), + utils (>= 4.3.2) From bbffd87fa1ecba293daedf706a8f117024ea8880 Mon Sep 17 00:00:00 2001 From: Sander Devisscher Date: Wed, 8 May 2024 12:54:50 +0200 Subject: [PATCH 8/9] fix typo #16 --- R/download_gdrive_if_missing.R | 4 ++-- man/download_gdrive_if_missing.Rd | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/R/download_gdrive_if_missing.R b/R/download_gdrive_if_missing.R index 4c64d33..4ac41bc 100644 --- a/R/download_gdrive_if_missing.R +++ b/R/download_gdrive_if_missing.R @@ -22,11 +22,11 @@ #' googledrive. #' #' @examples -#' #' \dontrun{ +#' \dontrun{ #' # download newest version of the team charter #' download_gdrive_if_missing(gfileID = "1gtqcZojPnbLhEgpul3r9sy2zK3UyyCVG", #' destfile = "../../Teamcharters/Teamcharter_FIS.pdf", -#' email = Sys.getenv("email") +#' email = Sys.getenv("email"), #' update_always = TRUE) #' } #' diff --git a/man/download_gdrive_if_missing.Rd b/man/download_gdrive_if_missing.Rd index 1e0ca1e..d8417e9 100644 --- a/man/download_gdrive_if_missing.Rd +++ b/man/download_gdrive_if_missing.Rd @@ -32,11 +32,11 @@ achieved by adding email as an item in a .renviron file or even beter by using more robust encryption methods. } \examples{ -#' \dontrun{ +\dontrun{ # download newest version of the team charter download_gdrive_if_missing(gfileID = "1gtqcZojPnbLhEgpul3r9sy2zK3UyyCVG", destfile = "../../Teamcharters/Teamcharter_FIS.pdf", - email = Sys.getenv("email") + email = Sys.getenv("email"), update_always = TRUE) } From 5d3b8a1cc13571fb8bf5539df3f55d1c0b9aa802 Mon Sep 17 00:00:00 2001 From: LynnINBO Date: Mon, 13 May 2024 11:07:20 +0200 Subject: [PATCH 9/9] Test with DRG export #18 --- R/download_gdrive_if_missing.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/R/download_gdrive_if_missing.R b/R/download_gdrive_if_missing.R index 4ac41bc..438b98f 100644 --- a/R/download_gdrive_if_missing.R +++ b/R/download_gdrive_if_missing.R @@ -29,7 +29,14 @@ #' 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,