#' Download list of URLs
#'
#' Download data from list of URLs.
#' @param x Vector of URLs.
#' @param destFolder Destination folder to save downloaded files.
#' @param mode See information for \code{download.file}.
#' @param cl Cluster object created through \code{parallel::makeCluster}.
#' @return Names of files.
#' @export
#' @examples
#' get_https(x = c("https://your/data/path.nc"), destFolder = getwd())
get_https <- function(x, destFolder = NULL, mode = "wb", cl = NULL){
if (is.null(destFolder)) stop("Please provide a destination folder ('destFolder' argument)")
if (!file.exists(destFolder)) stop("Destination Folder does not exist!")
#- print
cat(paste0("Downloading ", length(x), " files...\n"))
#- define download function
dl_fun <- function(y, destFolder, mode){
tryCatch({
#- Get filename
fname <- basename(y)
#- Add slash to folder if necessary
if (substr(destFolder, nchar(destFolder), nchar(destFolder)) != "/"){
destFolder <- paste0(destFolder, "/")
}
#- Create savepath
savePath <- paste0( destFolder, fname )
#- Download file
download.file(url = y,
destfile = savePath,
mode = mode,
quiet = T)
return(
data.frame(url = y,
file_path = savePath,
success = T)
)
}, error = function(e){
return(
data.frame(url = y,
file_path = NA,
success = F)
)
})
}
#- download list of files
path_list <- pbapply::pblapply(X = x,
FUN = dl_fun,
destFolder = destFolder,
mode = mode,
cl = cl)
#- return file path
return(
do.call("rbind", path_list)
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.