R中的Windows上的read.xls和url

时间:2016-06-08 14:24:07

标签: r windows macos xls

我在这里看到很多关于将read.xls与url一起使用的帖子,它们都在我的Mac上运行,但现在当我尝试在我的Windows计算机上使用该代码时,它无效。我在Mac上使用了以下代码:

tmp <- tempfile()
download.file("https://www.spdrs.com/site-content/xls/SPY_All_Holdings.xls?fund=SPY&docname=All+Holdings&onyx_code1=1286&onyx_code2=1700", destfile = tmp, method = "curl")
SPY <- read.xls(tmp, skip=3)

unlink(tmp)

使用“curl”不再工作(“有状态127”是警告信息),当我尝试“内部”或“wininet”时,它说“正式参数”方法“由多个实际参数匹配”。当我尝试read.xls时,它说文件“丢失”和“无效”。我已经下载了Perl,Java,gdata,Rcurl和“下载程序”软件包(因为我听说它可以更好地使用https)并且可以使用它来代替....在Windows计算机上我还需要做些什么来制作这段代码有用吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

> library(RCurl)
> library(XLConnect)
> URL <- "https://www.spdrs.com/site-content/xls/SPY_All_Holdings.xls?fund=SPY&docname=All+Holdings&onyx_code1=1286&onyx_code2=1700"
> f = CFILE("SPY_All_Holdings.xls", mode="wb")
> curlPerform(url = URL, writedata = f@ref, ssl.verifypeer = FALSE)
# OK 
# 0 
> close(f)
# An object of class "CFILE"
# Slot "ref":
# <pointer: (nil)>

> out <- readWorksheetFromFile(file = "SPY_All_Holdings.xls",sheet="SPY_All_Holdings")
> head(out)
#           Fund.Name. SPDR..S.P.500..ETF     Col3                   Col4        Col5
# 1          Ticker Symbol:                SPY     <NA>                   <NA>         <NA>
# 2               Holdings:   As of 06/06/2016     <NA>                   <NA>         <NA>
# 3                    Name         Identifier   Weight                 Sector  Shares Held
# 4              Apple Inc.               AAPL 2.945380 Information Technology 54545070.000
# 5   Microsoft Corporation               MSFT 2.220684 Information Technology 77807630.000
# 6 Exxon Mobil Corporation                XOM 1.998224                 Energy 40852760.000