首页 > 解决方案 > 从 R 中的 URL 下载和读取 Excel 文件时出错

问题描述

以下代码

库(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="数据", 跳过 = 7)

产生Error in sheets_fun(path) : Failed to open ie_data.xls
令我困惑的一件事是,如果转到 URL 并手动下载文件,我可以使用 read_xls 打开它。我认为问题可能与 download.file 功能有关。

我希望能够直接从 URL 读取此 Excel 文件,或者至少下载并读取它而无需手动执行。我在使用 R 3.5.1 和 readxl 版本 1.1.0 的 Window x86_64 系统上。谢谢。

标签: rexcel

解决方案


我仍然不知道为什么上面的代码不起作用。使用此SO post,我发现以下代码将起作用:

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)

推荐阅读