简体   繁体   English

使用 r-script 连接 NodeJS 和 R 的启动问题

[英]Starting problems connecting NodeJS and R using r-script

I am trying to call a R-script from a js-file but I always get null as a return value我正在尝试从 js 文件调用 R 脚本,但我总是将null作为返回值

My folder-structure is as following我的文件夹结构如下

-R_test -R_test
--example.js --example.js
--helloWorld.R --helloWorld.R

example.js looks as follows: example.js 如下所示:

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

var out = R('helloWorld.R')
    .callSync();

console.log(out);

and helloWorld.R looks as like this: helloWorld.R 看起来像这样:

print("Hello World")

Looking at the only other question I could find similar to mine here my syntax is correct.看看我在这里找到的与我相似的唯一其他问题我的语法是正确的。 Changing the js-file to将 js 文件更改为

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

var out = R('helloWorld.R')
    .data("Hello World)
    .callSync();

console.log(out);

and helloWorld.R to和 helloWorld.R 到

print(input)

should according to the readme of r-script print the data Hello World but it also just returns null应该根据r-scriptreadme打印数据Hello World但它也只返回null

If there is any better way to run a R-script from a nodeJS-file please let me know如果有更好的方法从 nodeJS 文件运行 R 脚本,请告诉我

Thanks to dcruvolo I got it working.感谢dcruvolo我让它工作了。 Here is how the js-file needs to look:以下是 js 文件需要的外观:

const { R } = require('@fridgerator/r-script')

let r = new R('./members.R')

r.call()
  .then(response => console.log(response))
  .catch(e => console.log('error : ', e))

And here is the .R-file:这是 .R 文件:

members <- c(129, 1, 5,  3,  2,  1,  21,  1,  8,  7,  0,  1,  1,  1,  110,  0,  0,  5,  1,  0,  0,  0)

membersSorted <- sort(members, decreasing = TRUE)

# Graph cars using blue points overlayed by a line 
plot(membersSorted, type="o", col="blue")

# Create a title with a red, bold/italic font
title(main="Members", col.main="red", font.main=4)

png(filename="members.png")
plot(membersSorted, type="o", col="blue")
dev.off()

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

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