Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can R read a zipped XLS file from a URL?

Tags:

r

xls

Is it possible to read an Excel file from an online ZIP file?

I have been trying something like I would do with read.csv:

nuts = url("http://ec.europa.eu/eurostat/ramon/documents/nuts/NUTS_2010.zip")
xlsx::read.xlsx(unz(nuts, "NUTS_2010.xls"), 1)
close(nuts)

… to no avail.

like image 624
Fr. Avatar asked Jul 27 '13 14:07

Fr.


1 Answers

It's a little less convenient, but how about:

basefn <- "NUTS_2010"
urlPath <- "http://ec.europa.eu/eurostat/ramon/documents/nuts/"
xlsFile <- paste0(basefn,".xls")
zipFile <- paste0(basefn,".zip")
download.file(paste0(urlPath,zipFile),zipFile)
unzip(zipFile)
## I had trouble with xlsx::read.xlsx, but gdata::read.xls was OK
## xlsx::read.xlsx(xlsFile,1)
gdata::read.xls(xlsFile)
unlink(zipFile)

You can always pack this into a readZipURL <- function(urlPath,basefn) {...} function if you want to do it on a regular basis (you might want to clean up the downloaded XLS file too ...)

like image 150
Ben Bolker Avatar answered Sep 21 '22 10:09

Ben Bolker