简体   繁体   English

使用r-script将JSON从NodeJS传递到R

[英]Passing JSON from NodeJS to R using r-script

I am trying to setup a simple connect between a NodeJS application and an R-script. 我正在尝试在NodeJS应用程序和R脚本之间建立简单的连接。 So far I've managed to set up the basic connection and the running of the cript, by using r-script (found on npm). 到目前为止,我已经通过使用r-script (位于npm上)成功地建立了基本连接和cript的运行。

However, I am not able to pass a simple json to the R so that it can be converted to a dataframe using jsonlite . 但是,我无法将简单的json传递给R,因此无法使用jsonlite将其转换为数据jsonlite

NodeJS Code: NodeJS代码:

var R = require("r-script");

var out = R("script.R")
  .data({"Name" : "Mario", "Age" : 32, "Occupation" : "Plumber"},  {"Name" : "Peach", "Age" : 21, "Occupation" : "Princess"}, {}, {"Name" : "Bowser", "Occupation" : "Koopa"})
  .callSync();

console.log(out);

script.R: 脚本R:

library("jsonlite")
mydf <- fromJSON(input[[1]])

This gives the output: 这给出了输出:

 'Argument 'txt' must be a JSON string, URL or file.'

I have tried removing the indexation of the vector (and give the full list to the fromJSON) but it also doesn't work. 我试过删除向量的索引(并将完整列表提供给fromJSON),但是它也不起作用。

Has anyone sucessfully passed JSON to an R script with this npm module? 有人通过此npm模块成功将JSON传递给R脚本吗?

Thanks in advance! 提前致谢!

EDIT: Also, if place the JSON between "'", it gives me "trailing errors" in spaces, and if they are removed, on the { char. 编辑:此外,如果将JSON放在“'”之间,则会在空格中给我“跟踪错误”,如果将其删除,请在{char上。

I have no idea about how R works, however the error could point to the fact that what you are trying to pass is a javascript object, not a JSON. 我不知道R的工作原理,但是错误可能指向您要传递的是JavaScript对象而不是JSON的事实。 Javascript objects look like but are not identical to JSONs (see discussion here Javascript object Vs JSON ) Javascript对象看起来像但与JSON不同(请参见此处的讨论Javascript对象与JSON

One thing that you can try is passing in the data function a JSON string by calling the JSON.stringify function on your objects. 您可以尝试做的一件事是,通过在对象上调用JSON.stringify函数,向数据函数传递JSON字符串。 Something like this: 像这样:

var R = require("r-script");

var out = R("script.R")
  .data(JSON.stringify({"Name" : "Mario", "Age" : 32, "Occupation" : "Plumber"}), JSON.stringify( {"Name" : "Peach", "Age" : 21, "Occupation" : "Princess"}),JSON.stringify ({}), JSON.stringify({"Name" : "Bowser", "Occupation" : "Koopa"}))
  .callSync();

console.log(out);

It's a longshot but it shows a general direction on which you can debug 这是一个远景,但它显示了可以调试的一般方向

I managed to found a simple solution, the Manos Agelidis answer did put me in the right direction. 我设法找到了一个简单的解决方案,Manos Agelidis的回答确实使我朝着正确的方向前进。

Basically, I have able to parse the input string using: 基本上,我可以使用以下方法解析输入字符串:

string = paste(input, collapse=",") 
string = paste("[",string,"]")
frame = fromJSON(string)

and in NodeJS: 在NodeJS中:

  var out = R("script.R")
  .data('{"Name":"Mario","Age":32,"Occupation":"Plumber"}',
  '{"Name":"Peach","Age":21,"Occupation":"Princess"}',
  '{}',
  '{"Name":"Bowser","Occupation":"Koopa"}')
  .callSync();

fromJSON requires a vector but in string form, and not a literal vector. fromJSON需要一个向量,但必须是字符串形式,而不是文字向量。 Therefore, what I needed to do what to create a string using the elements of the input and to add [ and ] to the begging and end of the string, respectively. 因此,我需要做些什么来使用input的元素创建一个字符串,并将[]分别添加到字符串的开头和结尾。 This did convert to a data frame properly. 这确实可以正确转换为数据帧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM