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

converting json file into R/text file without execution

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格式的文件,所需的信息存储在内容变量中。当尝试使用 readLines() or scan() 读取命令时,正在执行命令而不是将它们转换为简单文件。如何将其转换为 r 文件?

输出(?): r script/text 文件中的命令。

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

如果有人在寻找这个问题的答案,@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)]