【发布时间】: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