从R中的URL下载和读取Excel文件时出错 [英] Error downloading and reading Excel file from URL in R

查看:60
本文介绍了从R中的URL下载和读取Excel文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码

库(readxl)
网址<-" 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)

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)

在sheets_fun(路径)中产生错误:无法打开ie_data.xls
让我感到困惑的一件事是,如果转到URL并手动下载文件,则可以使用read_xls来打开它.我认为问题可能出在download.file函数上.

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.

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

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.

推荐答案

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

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)

这篇关于从R中的URL下载和读取Excel文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆