I have created the following function:
download_ <- function(zip_link, destination){
zip_url <- zip_link
zip_file <- file.path(destination, basename(zip_link))
download.file(zip_url, destfile = zip_file, cacheOK = FALSE)
}
download_(zip_link = "https://datosabiertos.mineduc.cl/wp-content/uploads/2022/09/Asistencia-declarada-Abril-2022.rar",
destination = "C:/Users/####/Desktop")
And it downloads the April 2022 file like this:
But if I download it manually:
What's going on here? What is my code doing wrong?
Thanks in advance
When downloading binary files in Windows with download.file()
, a proper mode should be set with mode = "wb"
, so the call becomes:
download.file(zip_url, destfile = zip_file, cacheOK = FALSE, mode ="wb")
It can get bit confusing at times as for certain URL patterns it is set automagically and downloaded files can end up OK, i.e. URLs ending with ".zip". It also wouldn't hurt to always use binary mode. Or use something with more practical defaults, like curl::curl_download()
.
From ?download.file()
:
The choice of binary transfer (mode = "wb" or "ab") is important on Windows, since unlike Unix-alikes it does distinguish between text and binary files and for text transfers changes \n line endings to \r\n (aka ‘CRLF’).
On Windows, if mode is not supplied (missing()) and url ends in one of .gz, .bz2, .xz, .tgz, .zip, .jar, .rda, .rds or .RData, mode = "wb" is set so that a binary transfer is done to help unwary users.
Code written to download binary files must use mode = "wb" (or "ab"), but the problems incurred by a text transfer will only be seen on Windows.