Importing multiple online XLS files into R

Viewed 22

I am trying to imports multiple XSL files which are located on a website. These files represent data for different years, they have similar names and the only part of the url that changes ins the year at the end of url, e.g.:

https://www.nbs.sk/_img/Documents/STATIST/URSADZ/PUM/0710-01-2020.XLS
https://www.nbs.sk/_img/Documents/STATIST/URSADZ/PUM/0710-01-2019.XLS
https://www.nbs.sk/_img/Documents/STATIST/URSADZ/PUM/0710-01-2018.XLS

I created a following code to import them but I got a below error.

years <- format((seq(as.Date("2009-1-1"), today(), by = "years")), format = "%Y")
year.urls <- paste0("https://www.nbs.sk/_img/Documents/STATIST/URSADZ/PUM/0710-01-", years, ".XLS")
GET(year.urls, write_disk(TF <- tempfile(fileext = ".xls")))

Error in parse_url(url) : length(url) == 1 is not TRUE

The code works if I import only one XLS file but I cannot figure out how to import several files at once. My aim is to have a list and then call a specific object (XLS file for a selected year) from the list to perform further actions. Thank you!

1 Answers

Using gdata library.

library(gdata)

years <- format((seq(as.Date("2010-1-1"), today(), by = "years")), format = "%Y")

for (i in years) {
  download.file(paste0("https://www.nbs.sk/_img/Documents/STATIST/URSADZ/PUM/0710-01-", i, ".XLS"), 
                destfile=paste0("base",i,".xls"))
}

Related