r - 从 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 系统上。谢谢。
解决方案
我仍然不知道为什么上面的代码不起作用。使用此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)
推荐阅读
- code-coverage - 如何在 SanitizerCoverage 中/为 SanitizerCoverage 定义自己的警卫以提供 libfuzzer 的覆盖范围
- javascript - 在 VS Code 和 JSDoc 中对 protobuf 生成的 JS 代码进行语义检查
- spring - 如何在 Spring 框架中测试 websocket?
- angular - 元素隐含地具有“任何”类型,因为 ANGULAR 中的类型表达式
- mysql - MySQL InnoDB 磁盘写入在 2.5 小时后突然增加
- swift - 如何修复不推荐使用的子字符串(带有:)
- oracle - Oracle 触发器中的 RTTI
- javascript - Ember 路由不显示模板内容
- android - Nordic SDK在哪里接收数据?
- specflow - Specflow 表与场景大纲