【问题标题】:How to simply download an Excel File directly from the Internet with R如何使用 R 直接从 Internet 下载 Excel 文件
【发布时间】:2020-01-08 11:43:37
【问题描述】:

我有一个我想下载的文件的 URL,并从这里直接读入 R。它是一个来自 Gapminder 的 .xlsx 文件。找到它的网站是“https://github.com/Gapminder-Indicators/u5mr/blob/master/u5mr-by-gapminder.xlsx”。

我尝试了两件事:

url <- "https://www.gapminder.org/data/documentation/gd005/u5mr-by-gapminder.xlsx"
download.file(url,destfile="example.xlsx")
example <- read_excel("example.xlsx")

有错误

> url <- "https://github.com/Gapminder-Indicators/u5mr/blob/master/u5mr-by-gapminder.xlsx"
> download.file(url,destfile="example.xlsx")
trying URL 'https://github.com/Gapminder-Indicators/u5mr/blob/master/u5mr-by-gapminder.xlsx'
Content type 'text/html; charset=utf-8' length unknown
downloaded 61 KB

> example <- read_excel("example.xlsx")
Error: Evaluation error: zip file 'C:\Users\user\Documents\example.xlsx' cannot be opened.
> 

library(RCurl)
URL <- "https://www.gapminder.org/data/documentation/gd005/u5mr-by-gapminder.xlsx"
x <- getURL(URL)
out <- read.csv(textConnection(x))
head(out[1:6])

结果

> library(RCurl)
> URL <- "https://www.gapminder.org/data/documentation/gd005/u5mr-by-gapminder.xlsx"
> x <- getURL(URL)
> out <- read.csv(textConnection(x))
Warning message:
In scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  :
  EOF within quoted string
> head(out[1:6])
Error in `[.data.frame`(out, 1:6) : undefined columns selected

所以文件要么根本没有下载,要么读入不正确。我怎样才能简单地从网页上下载一个excel文件并读入?

【问题讨论】:

    标签: r


    【解决方案1】:

    尽管有 URL,但 GitHub 服务器实际上并不提供文件,除非 raw=true 作为参数传入。

    改用这个网址:https://github.com/Gapminder-Indicators/u5mr/blob/master/u5mr-by-gapminder.xlsx?raw=true

    【讨论】:

    • 谢谢。它确实适用于下载文件。但是,它只下载 excel 文件的第一张表。因此,我无法将数据读入 R。我必须在您提供的 URL 中添加什么才能访问和下载名为“countries_and_territories”的文件的第二页?
    • @BenMat 整个文件被下载。 read_excel 一次只能阅读一张纸。您可以使用sheet 参数指定哪个工作表。
    【解决方案2】:

    我认为仅仅像@James 所说的那样更改 url 是不够的,(请注意,有问题的 url 会将您的浏览器转发到带有 actual 文件的不同 url - 在您的情况下是 @ 987654321@)。

    但这不是唯一的问题。您需要使用mode = "wb" 将下载的文件写入二进制格式,然后才能使用read_excel 打开它,同时指定要打开的工作表。

    这是一个工作示例:

    library(readxl)
    
    destfile <- path.expand("~/example.xlsx")
    url      <- paste0("https://raw.githubusercontent.com/Gapminder-Indicators",
                        "/u5mr/master/u5mr-by-gapminder.xlsx")
    
    download.file(url, destfile = destfile, mode = "wb")
    example <- read_excel(destfile, sheet = 2)
    

    现在您将电子表格作为小标题:

    print(example)
    #> # A tibble: 275 x 305
    #>    geo.name indicator.name geo   indicator `1800` `1801` `1802` `1803` `1804` `1805`
    #>    <chr>    <chr>          <chr> <chr>      <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
    #>  1 Abkhazia Child mortali~ abkh  u5mr         NA     NA     NA     NA     NA     NA 
    #>  2 Afghani~ Child mortali~ afg   u5mr        469.   469.   469.   469.   469.   469.
    #>  3 Akrotir~ Child mortali~ akr_~ u5mr         NA     NA     NA     NA     NA     NA 
    #>  4 Albania  Child mortali~ alb   u5mr        375.   375.   375.   375.   375.   375.
    #>  5 Algeria  Child mortali~ dza   u5mr        460.   460.   460.   460.   460.   460.
    #>  6 America~ Child mortali~ asm   u5mr         NA     NA     NA     NA     NA     NA 
    #>  7 Andorra  Child mortali~ and   u5mr         NA     NA     NA     NA     NA     NA 
    #>  8 Angola   Child mortali~ ago   u5mr        486.   486.   486.   486.   486.   486.
    #>  9 Anguilla Child mortali~ aia   u5mr         NA     NA     NA     NA     NA     NA 
    #> 10 Antigua~ Child mortali~ atg   u5mr        474.   470.   466.   462.   458.   455.
    #> # ... with 265 more rows, and 295 more variables: `1806` <dbl>, `1807` <dbl>
    

    【讨论】:

    • 这很好用,在对read_excel() 的调用中需要注意sheet = 2sheet = 'countries_and_territories'
    猜你喜欢
    • 1970-01-01
    • 2014-02-18
    • 2016-12-23
    • 2011-09-09
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 2014-12-10
    • 2014-08-27
    相关资源
    最近更新 更多