Error downloading and reading Excel file from URL in R [duplicate]

假如想象 提交于 2021-01-29 08:59:30

问题


The following code

library(readxl)
url <- "http://www.econ.yale.edu/~shiller/data/ie_data.xls"
destfile <- "ie_data.xls"
download.file(url, destfile)
ie_data <- read_xls(destfile, sheet="Data", skip = 7)

produces Error in sheets_fun(path) : Failed to open ie_data.xls
One thing that perplexes me is that if goto the URL and download the file manually I can use read_xls to open it. I think the issue may be with download.file function.

I'd like to be able to read this Excel file directly from the URL or at least download it and read it without doing so manually. I'm on a Window x86_64 system using R 3.5.1 and readxl version 1.1.0. Thanks.


回答1:


I still don't know why the code above doesn't work. Using this SO post, I find that the following code will work:

library(httr)
library(readxl)
url <- "http://www.econ.yale.edu/~shiller/data/ie_data.xls"
GET(url, write_disk(tf <- tempfile(fileext = ".xls")))
ie_data <- read_excel(tf, sheet="Data", skip = 7)



回答2:


Because you are using windows you have to specify binary mode

download.file(url, destfile, mode="wb")


来源:https://stackoverflow.com/questions/53330861/error-downloading-and-reading-excel-file-from-url-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!