是否可以从在线ZIP文件中读取Excel文件?

我一直在尝试类似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)

……无济于事。

最佳答案

不太方便,但是如何:

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)

如果您想定期执行此操作,则始终可以将其打包到readZipURL <- function(urlPath,basefn) {...}函数中(您可能也希望清理下载的XLS文件...)

关于r - R可以从URL读取压缩的XLS文件吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17898729/

10-12 17:12