将json文件转换为R/text文件而不执行 [英] converting json file into R/text file without execution

查看:45
本文介绍了将json文件转换为R/text文件而不执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Rstudio has crashed while working and the unsaved files were not able to be loaded into the session. But the files are available in the JSON format. An example,

{
    "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"
}

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

The JSON format files can be read using the jsonlite::fromJSON and the required information was stored in contents variable. When tried to read the commands using the readLines() or scan() the commands were being executed instead of converting them into a simple file. How to convert this into a r file ?

output(?): r 脚本/文本文件中的命令.

output(?):command in a r script/text file.

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

推荐答案

如果有人在寻找这个问题的答案,@凯文 工作了.

If anyone looking for answer to this question, the command suggested by @Kevin worked.

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

这篇关于将json文件转换为R/text文件而不执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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