his*_*eim 49 r data-manipulation data-management
我正在尝试从github读取一个CSV到R:
latent.growth.data <- read.csv("https://github.com/aronlindberg/latent_growth_classes/blob/master/LGC_data.csv")
Run Code Online (Sandbox Code Playgroud)
但是,这给了我:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : unsupported URL scheme
Run Code Online (Sandbox Code Playgroud)
我试过?read.csv
,?download.file
,getURL
(只有返回奇怪的HTML),以及在数据导入手册,但仍然无法知道如何使它发挥作用.
我究竟做错了什么?
A5C*_*2T1 89
试试这个:
library(RCurl)
x <- getURL("https://raw.github.com/aronlindberg/latent_growth_classes/master/LGC_data.csv")
y <- read.csv(text = x)
Run Code Online (Sandbox Code Playgroud)
你有两个问题:
RCurl
来解决它.在某些情况下(不是使用Github),你可以简单地用http替换https,然后就可以解决问题了,所以你总是可以先尝试一下,但我觉得使用RCurl是可靠的而不是太多的额外打字.Pau*_*tra 23
来自以下文件url
:
请注意,不支持"https://"连接(在Windows上有一些例外).
所以问题是R不允许对https
URL 进行同步.
你可以用download.file
与curl
:
download.file("https://raw.github.com/aronlindberg/latent_growth_classes/master/LGC_data.csv",
destfile = "/tmp/test.csv", method = "curl")
Run Code Online (Sandbox Code Playgroud)
Mac*_*iej 15
我正在使用R 3.0.2并且此代码完成了这项工作.
urlfile<-'https://raw.github.com/aronlindberg/latent_growth_classes/master/LGC_data.csv'
dsin<-read.csv(urlfile)
Run Code Online (Sandbox Code Playgroud)
这也是
urlfile<-'https://raw.github.com/aronlindberg/latent_growth_classes/master/LGC_data.csv'
dsin<-read.csv(url(urlfile))
Run Code Online (Sandbox Code Playgroud)
编辑(sessionInfo)
R version 3.0.2 (2013-09-25)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=Polish_Poland.1250 LC_CTYPE=Polish_Poland.1250
[3] LC_MONETARY=Polish_Poland.1250 LC_NUMERIC=C
[5] LC_TIME=Polish_Poland.1250
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.0.2
Run Code Online (Sandbox Code Playgroud)
意识到问题已经很久了,谷歌仍然认为这是最好的结果(至少对我而言)所以我决定提供2015年的答案.
如r-bloggers描述的那样,人们现在通常会迁移到curl
包(包括名人httr
),它提供了以下非常简单的解决方案:
library(curl)
x <- read.csv( curl("https://raw.githubusercontent.com/trinker/dummy/master/data/gcircles.csv") )
Run Code Online (Sandbox Code Playgroud)
与akhmed类似,我想我会更新答案,因为现在你可以使用Hadley的readr
包.需要注意的一件事是:您需要将url作为原始内容(请参阅//raw.git...
下文).这是一个例子:
library(readr)
data <- read_csv("https://raw.githubusercontent.com/RobertMyles/Bayesian-Ideal-Point-IRT-Models/master/Senate_Example.csv")
Run Code Online (Sandbox Code Playgroud)
瞧!