将在线xlsx文件读入R

时间:2015-01-19 18:11:21

标签: r

我正在尝试将电子表格从AQR数据库直接下载到R. 我有这个链接:http://www.aqr.com/~/media/files/data-sets/value-and-momentum-everywhere-portfolios-monthly.xlsx提示下载。但是,在尝试以下代码时:

> url1<-"http://www.aqr.com/~/media/files/data-sets/value-and-momentum-everywhere-portfolios-monthly.xlsx"
> download.file(url1,destfile="example.xlsx")

我收到此错误

trying URL 'http://www.aqr.com/~/media/files/data-sets/value-and-momentum-everywhere-portfolios-monthly.xlsx'
Error in download.file(url1, destfile = "example.xlsx") : cannot open URL 'http://www.aqr.com/~/media/files/data-sets/value-and-momentum-everywhere-portfolios-monthly.xlsx'

https://www.aqr.com/library/data-sets/value-and-momentum-everywhere-portfolios-monthly是我尝试下载数据的页面(在完整设置数据链接下)。

你能提供一些指导吗?

2 个答案:

答案 0 :(得分:1)

我不太确定是什么原因导致了这个问题,但以下情况对我有用:

library(XLConnect)
## 
con <- "http://www.aqr.com/~/media/files/data-sets/value-and-momentum-everywhere-portfolios-monthly.xlsx"
download.file(con,"xlsxFile.xlsx",mode="wb")
##
newWB <- loadWorkbook(
  file="xlsxFile.xlsx",
  create=F)
##
R> getSheets(newWB)
[1] "VME Portfolios" "Definitions"    "Data Sources"   "Disclosures"

以下是下载文件的屏幕截图:

enter image description here

答案 1 :(得分:1)

看起来该链接重定向到https,download.file默认情况下不支持。如果您安装了wget或curl,则可以使用

download.file("https://www.aqr.com/~/media/files/data-sets/value-and-momentum-everywhere-portfolios-monthly.xlsx", 
              "example.xlsx", 
              method = "wget")

download.file("https://www.aqr.com/~/media/files/data-sets/value-and-momentum-everywhere-portfolios-monthly.xlsx", 
              "example.xlsx", 
              method = "curl")

Download a file from HTTPS using download.file()

讨论了这些和其他选项