R产生“不支持的URL方案".从https站点获取数据时出错 [英] R produces "unsupported URL scheme" error when getting data from https sites

查看:97
本文介绍了R产生“不支持的URL方案".从https站点获取数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R版本3.0.1(2013-05-16)

R version 3.0.1 (2013-05-16) for Windows 8 knitr version 1.5 Rstudio 0.97.551

我正在使用knitr进行我的R代码的减价. 作为分析的一部分,我从网络上下载了各种数据集,knitr可以从http站点获取数据,而从https站点获取数据,则可以生成unsupported URL scheme消息,这完全可以. 我知道在Mac上使用download.file函数时,必须将method参数设置为curl才能从https获取数据,但这在使用knitr时无济于事.

I am using knitr to do the markdown of my R code. As part of my analysis I downloaded various data sets from the web, knitr is totally fine with getting data from http sites but from https ones where it generates an unsupported URL scheme message. I know when using the download.file function on a mac the method parameter has to be set to curl to get data from an https however this doesn't help when using knitr.

我需要怎么做才能使knitr从Https网站收集数据?

What do I need to do so that knitr will gather data from Https websites?

这是在Knitr中返回错误的代码块,但是在R中运行时不会出错.

Here is the code chunk that returns an error in Knitr but when run through R works without error.

```{r}
fileurl <- "https://dl.dropbox.com/u/7710864/data/csv_hid/ss06hid.csv"
download.file(fileurl, destfile = "C:/Users/xxx/yyy")
```

推荐答案

编辑(2016年5月):自R 3.3.0起,download.file()应该在所有平台上自动处理SSL网站,该答案的其余部分无济于事.

Edit (May 2016): As of R 3.3.0, download.file() should handle SSL websites automatically on all platforms, making the rest of this answer moot.

您想要这样的东西:

library(RCurl)
data <- getURL("https://dl.dropbox.com/u/7710864/data/csv_hid/ss06hid.csv",
               ssl.verifypeer=0L, followlocation=1L)

将数据作为单个字符串读入内存.您仍然必须以某种方式将其解析为数据集.一种策略是:

That reads the data into memory as a single string. You'll still have to parse it into a dataset in some way. One strategy is:

writeLines(data,'temp.csv')
read.csv('temp.csv')

您也可以直接将数据分离出来,而无需写入文件:

You can also separate out the data directly without writing to file:

read.csv(text=data)

实际上,使用 rio 软件包更容易得多:

A much easier option is actually to use the rio package:

library("rio")
import("https://dl.dropbox.com/u/7710864/data/csv_hid/ss06hid.csv")

这将直接从HTTPS URL读取并返回一个data.frame.

This will read directly from the HTTPS URL and return a data.frame.

这篇关于R产生“不支持的URL方案".从https站点获取数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆