R 中 Windows 上的 read.xls 和 url
read.xls and url on Windows in R
我在这里看到很多关于将 read.xls 与 url 一起使用的帖子,它们都适用于我的 Mac,但现在当我尝试在我的 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" 不再有效("had status 127" 是警告消息),当我尝试 "internal" 或 "wininet" 时,它说“正式论证 "method" 匹配多个实参”。当我尝试 read.xls 时,它说文件是 "missing" 和 "invalid"。我已经下载了 Perl、Java、gdata、Rcurl 和 "downloader" 包(因为我听说它在 https 上效果更好)并且可以使用它来代替......还有其他东西吗我必须在 Windows 计算机上做才能使此代码正常工作?
谢谢!
> 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
我在这里看到很多关于将 read.xls 与 url 一起使用的帖子,它们都适用于我的 Mac,但现在当我尝试在我的 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" 不再有效("had status 127" 是警告消息),当我尝试 "internal" 或 "wininet" 时,它说“正式论证 "method" 匹配多个实参”。当我尝试 read.xls 时,它说文件是 "missing" 和 "invalid"。我已经下载了 Perl、Java、gdata、Rcurl 和 "downloader" 包(因为我听说它在 https 上效果更好)并且可以使用它来代替......还有其他东西吗我必须在 Windows 计算机上做才能使此代码正常工作?
谢谢!
> 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