R read.csv 来自 URL knitr 错误
R read.csv from URL error in knitr
当我在 R 控制台中 运行 这段代码时,它工作正常:
read.csv('https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')
但是当我尝试将其放入 R markdown 文档并编织它时,我得到以下信息:
Error in open.connection(file, "rt") : cannot open the connection
Calls: <Anonymous> ... eval -> read.csv -> read.table -> open -> open.connection
Execution halted
我也尝试过使用 http
和 url()
,但都没有用
read.csv('http://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')
read.csv(url('http://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv'))
两者在通常的 R 会话中都工作正常。
如何编织?我希望有一些方法可以避免下载文件并将其放置在工作目录中的某个位置。
出于链接目的:这与 read.table() and read.csv both Error in Rmd 中的问题相同,但在我的情况下,我试图从 url 中读取,而不是从文件中读取。
因此根据您的数据调整@Thomas 的回答,下面的方法就可以了。
library(RCurl)
data <- getURL("https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv",
ssl.verifypeer=0L, followlocation=1L)
read.csv(text=data)
您还可以查看 Error when knitr has to download a zip file,其中将 https 替换为 http 有帮助。
@puslet88 的回答有帮助,但我稍微修改了一下 "intrusive"。
所以我所做的是将以下内容放在 Rmd fine 的开头:
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(RCurl)
read.csv.orig = read.csv
read.csv = function(file, ...) {
if (is.character(file)) {
if (grepl('^https://', file)) {
data = getURL(file, ssl.verifypeer=0L, followlocation=1L)
return (read.csv.orig(text=data, ...))
} else if (grepl('^http://', file)) {
data = getURL(file)
return (read.csv.orig(text=data, ...))
} else {
return (read.csv.orig(file, ...))
}
} else {
return (read.csv.orig(file, ...))
}
}
```
现在我不必在我的 R markdown 文档中更改对 read.csv
的所有调用,我像以前一样使用它:
read.csv('https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')
当我在 R 控制台中 运行 这段代码时,它工作正常:
read.csv('https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')
但是当我尝试将其放入 R markdown 文档并编织它时,我得到以下信息:
Error in open.connection(file, "rt") : cannot open the connection
Calls: <Anonymous> ... eval -> read.csv -> read.table -> open -> open.connection
Execution halted
我也尝试过使用 http
和 url()
,但都没有用
read.csv('http://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')
read.csv(url('http://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv'))
两者在通常的 R 会话中都工作正常。
如何编织?我希望有一些方法可以避免下载文件并将其放置在工作目录中的某个位置。
出于链接目的:这与 read.table() and read.csv both Error in Rmd 中的问题相同,但在我的情况下,我试图从 url 中读取,而不是从文件中读取。
因此根据您的数据调整@Thomas 的回答,下面的方法就可以了。
library(RCurl)
data <- getURL("https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv",
ssl.verifypeer=0L, followlocation=1L)
read.csv(text=data)
您还可以查看 Error when knitr has to download a zip file,其中将 https 替换为 http 有帮助。
@puslet88 的回答有帮助,但我稍微修改了一下 "intrusive"。 所以我所做的是将以下内容放在 Rmd fine 的开头:
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(RCurl)
read.csv.orig = read.csv
read.csv = function(file, ...) {
if (is.character(file)) {
if (grepl('^https://', file)) {
data = getURL(file, ssl.verifypeer=0L, followlocation=1L)
return (read.csv.orig(text=data, ...))
} else if (grepl('^http://', file)) {
data = getURL(file)
return (read.csv.orig(text=data, ...))
} else {
return (read.csv.orig(file, ...))
}
} else {
return (read.csv.orig(file, ...))
}
}
```
现在我不必在我的 R markdown 文档中更改对 read.csv
的所有调用,我像以前一样使用它:
read.csv('https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')