Jim O.
Jim O.

Reputation: 1111

Download multiple excel files linked through urls in R

I have a list containing hundreds of URLs directly linking to .xlsx files for a download:

list <- c("https://ec.europa.eu/consumers/consumers_safety/safety_products/rapex/alerts/?event=main.weeklyReport.Excel&web_report_id=980", 
          "https://ec.europa.eu/consumers/consumers_safety/safety_products/rapex/alerts/?event=main.weeklyReport.Excel&web_report_id=981", 
          "https://ec.europa.eu/consumers/consumers_safety/safety_products/rapex/alerts/?event=main.weeklyReport.Excel&web_report_id=990")

To download everything in the list, I created a loop:

for (url in list) {
  download.file(url, destfile = "Rapex-Publication.xlsx", mode="wb")
}

However, it only downloads the first file and not the rest. My guess is that the program is overwriting the same destfile. What would I have to do to circumvent this issue?

Upvotes: 0

Views: 819

Answers (1)

Robert Tan
Robert Tan

Reputation: 674

Try something along the lines of:

for (i in 1:length(list)) {
  download.file(list[i], destfile = paste0("Rapex-Publication-", i, ".xlsx"),  mode="wb")
}

Upvotes: 2

Related Questions