Hello I am trying to perform a task similar to this one but instead of downloading csv I want to download tif files.
I do not know what I am doing wrong but I tried with both passying otherwise=NULL
to safely and with a print message ... in every case when it comes to files that do not exist (e.g. 29 February wbgtmax.2015.02.29.tif ) I got an error message
Caused by error in `download.file()`:
! cannot open URL 'https://data.chc.ucsb.edu/people/cascade/UHE-daily/wbgtmax/2015/wbgtmax.2015.02.29.tif'
Run `rlang::last_trace()` to see where the error occurred.
Warning messages:
1: In download.file(url = x, destfile = y, mode = "wb") :
downloaded length 0 != reported length 0
2: In download.file(url = x, destfile = y, mode = "wb") :
cannot open URL 'https://data.chc.ucsb.edu/people/cascade/UHE-daily/wbgtmax/2015/wbgtmax.2015.02.29.tif': HTTP status was '404 Not Found'
This is my code
library(glue)
library(purrr)
library(dplyr)
getOption('timeout') # Look at the timeout option and increase it
options(timeout=300)
days=c("01","02","03","04","05","06","07","08","09","10","11","12","13","14","15",
"16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31")
months=c("01","02","03","04","05","06","07","08","09","10","11","12")
# Creates a String of the URL Addresses
urls <-
tidyr::expand_grid(months, days) |>
glue_data("https://data.chc.ucsb.edu/people/cascade/UHE-daily/wbgtmax/2015/wbgtmax.2015.{months}.{days}.tif")
head(urls, 5)
# Creates Names for the Files
tif_names <-
tidyr::expand_grid(months, days) %>%
glue_data("wbgtmax.2015.{months}.{days}.tif")
setwd("C:/Users/angel/Documents/wbgtmax/2015")
safe_download=function(x,y){
safely(download.file(url=x,destfile = y,mode = "wb"),
otherwise = print(glue("{x} file not found")))}
walk2(urls,tif_names,\(x,y)safe_download(x=x,y=y))
Can anyone help me understand what I am doing wrong? I have already used safely in the past but never come to such issue
Thanks