问题
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