【问题标题】:Error downloading and reading Excel file from URL in R [duplicate]从 R 中的 URL 下载和读取 Excel 文件时出错 [重复]
【发布时间】:2018-11-16 03:08:36
【问题描述】:

以下代码

库(readxl)
网址 http://www.econ.yale.edu/~shiller/data/ie_data.xls"
目标文件 下载.file(url, destfile)
ie_data

产生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 系统上。谢谢。

【问题讨论】:

    标签: r excel


    【解决方案1】:

    我仍然不知道为什么上面的代码不起作用。使用这个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)
    

    【讨论】:

      【解决方案2】:

      因为你使用的是windows,所以必须指定二进制模式

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

      【讨论】:

      • 出于某种原因,SO 在我的代码中加入了智能引号。只需使用普通引号
      • prbly macOS。你可以为 SO 文本框关闭它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 2013-07-11
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多