R read.csv from URL error in knitr

最后都变了- 提交于 2019-12-12 04:55:33

问题


When I run this code in the R console, it works fine:

read.csv('https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')

But when I try to put in into an R markdown document and knit it, I get the following:

Error in open.connection(file, "rt") : cannot open the connection
Calls: <Anonymous> ... eval -> read.csv -> read.table -> open -> open.connection
Execution halted

I also tried using http and url(), but neither helped

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'))

Both work fine in usual R sessions.

How can knitr? I hope there are ways that avoid downloading the file and placing it somewhere into working directory.

For linking purposes: It's the same issue as in read.table() and read.csv both Error in Rmd, but it my case I'm trying to read from an url, not from a file.


回答1:


Thus adapting @Thomas's answer to your data, the following does the trick.

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)

You may also check Error when knitr has to download a zip file, where dropping https for http helped.




回答2:


Answer of @puslet88 helped, but I modified it slightly to be less "intrusive". So what I did was putting the following at the beginning of the 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, ...))
  }
}
```

Now I don't have to change all the calls to read.csv in my R markdown document, I use it as before:

read.csv('https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')


来源:https://stackoverflow.com/questions/28997402/r-read-csv-from-url-error-in-knitr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!