直接从 github 加载 .RData

Directly loading .RData from github

我想从 https://github.com/myaseen208/PakPMICS2018Data/ 加载 PakPMICS2018bh.RData 数据并使用了以下引发错误的代码:

library(RCurl)
PakPMICS2018bhURL <- "https://github.com/myaseen208/PakPMICS2018Data/raw/master/PakPMICS2018bh.RData"
load(url(PakPMICS2018bhURL))

Error in load(url(PakPMICS2018bhURL)) : the input does not start with a magic number compatible with loading from a connection

我想知道我的代码有什么问题。请帮忙。

你可以试试这个:

只需确保您设置了工作目录。

setwd("SET YOUR Working Directory - the file will download here")
working_directory <- getwd()
if (!file.exists("PakPMICS2018bh.RData")) {
  download.file(   "https://github.com/myaseen208/PakPMICS2018Data/raw/master/PakPMICS2018bh.RData",   "PakPMICS2018bhURL.RData")

  load(file.path(working_directory, "PakPMICS2018bhURL.RData"))
  } 

问题不在您的代码中,它应该可以正常工作。 例如,这通常从 github

加载一个 Rdata 文件
load(url("https://github.com/mawp/spict/raw/master/spict/data/pol.rda"))

您的问题来自于您尝试打​​开的文件,它们以 R 版本 3.5 中引入的序列化格式 3 保存,使用 save(version = 3)

R has new serialization format (version 3) which supports custom serialization of ALTREP framework objects. These objects can still be serialized in format 2, but less efficiently. Serialization format 3 also records the current native encoding of unflagged strings and converts them when de-serialized in R running under different native encoding. Format 3 comes with new serialization magic numbers (RDA3, RDB3, RDX3). Format 3 can be selected by version = 3 in save(), serialize() and saveRDS(), but format 2 remains the default for all serialization and saving of the workspace. Serialized data in format 3 cannot be read by versions of R prior to version 3.5.0.

编辑

经过更多研究,我认为这是一个错误(或功能?)。 对于使用等于 FALSETRUEgzcompression 参数保存的文件,代码在 R 版本 >= 3.5 中按预期工作。但是对于等于 xz 的压缩,这似乎是你的情况,它不起作用。

有两种选择:使用 gz 压缩保存文件,或者使用@user113156 的回答中的解决方法。

如果可以 read 而不是 load 文件,这对我有用:

readRDS(url("https://.../FILENAME.rda"))