将json文件转换为R / text文件而不执行

时间:2016-11-03 12:38:21

标签: json r rstudio

Rstudio在工作时崩溃,未保存的文件无法加载到会话中。但这些文件以JSON格式提供。一个例子,

{
    "contents" : "library(hgu133a.db)\nx <- hgu133aENSEMBL\nx\nlength(x)\ncount.mappedkeys(x)\nx[1:3]\nlinks(x[1:3])\n\n## Keep only the mapped keys\nkeys(x) <- mappedkeys(x)\nlength(x)\ncount.mappedkeys(x)\nx # now it is a submap\n\n## The above subsetting can also be achieved with\nx <- hgu133aENSEMBL[mappedkeys(hgu133aENSEMBL)]\n\n",
    "created" : 1463131195093.000,
    "dirty" : true,
    "encoding" : "",
    "folds" : "",
    "hash" : "1482602869",
    "id" : "737C178C",
    "lastKnownWriteTime" : 0,
    "path" : null,
    "project_path" : null,
    "properties" : {
        "tempName" : "Untitled3"
    },
    "source_on_save" : false,
    "type" : "r_source"
}

可以使用jsonlite::fromJSON读取JSON格式文件,并将所需信息存储在contents变量中。当尝试使用readLines() or scan()读取命令时,正在执行命令而不是将它们转换为简单文件。如何将其转换为r文件?

输出(?):r脚本/文本文件中的命令。

library(hgu133a.db)
x <- hgu133aENSEMBL
x
length(x)
count.mappedkeys(x)
x[1:3]
links(x[1:3])

## Keep only the mapped keys
keys(x) <- mappedkeys(x)
length(x)
count.mappedkeys(x)
x 
# now it is a submap

## The above subsetting can also be achieved with
x <- hgu133aENSEMBL[mappedkeys(hgu133aENSEMBL)]

1 个答案:

答案 0 :(得分:0)

如果有人在寻找这个问题的答案,@ Kevin建议的命令就可以了。

writeLines(json$contents, con = "/path/to/file.R")

输出:

library(hgu133a.db)
x <- hgu133aENSEMBL
x
length(x)
count.mappedkeys(x)
x[1:3]
links(x[1:3])

## Keep only the mapped keys
keys(x) <- mappedkeys(x)
length(x)
count.mappedkeys(x)
x # now it is a submap

## The above subsetting can also be achieved with
x <- hgu133aENSEMBL[mappedkeys(hgu133aENSEMBL)]