简体   繁体   English

R可以从URL读取压缩的XLS文件吗?

[英]Can R read a zipped XLS file from a URL?

Is it possible to read an Excel file from an online ZIP file? 是否可以从在线ZIP文件中读取Excel文件?

I have been trying something like I would do with read.csv : 我一直在尝试类似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. ……无济于事。

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

This worked for me 这对我有用

options( java.parameters = "-Xmx4g" )
library(XLConnect)
temp <- tempfile()
zipfile <- paste0("https://www.cms.gov/Research-Statistics-Data-and-Systems/Statistics-Trends-and-Reports/MCRAdvPartDEnrolData/Downloads/"
                  ,year,"/",year,"-", "Low-Income-Subsidy-Contract-Enrollment-by-County.zip")
download.file(zipfile,temp)

filename <- paste0("WEB Tables LIS by state -county ",month2,"-","1","-",year,".xlsx")

temp1 <- unzip(temp)
assign(paste0("scc_pdp_lis_",year),readWorksheetFromFile(temp1,sheet=1,startRow=7,header=F))

I am reading some CMS files I was interested on 我正在阅读一些我感兴趣的CMS文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM