How to call `var iris = console.r.get("iris");` from node JS?
emmansh opened this issue · 4 comments
I've just found this V8
library and am trying to wrap my head around it. I want to call R from node JS script. How should I do it?
I've tried to first open R
and typed:
library(V8)
ctx <- v8()
ctx$console()
Then, I opened a separate text editor and wrote a simple js command
// in js
var iris = console.r.get("iris");
console.log(iris)
Then ran the script with node
, and got:
var iris = console.r.get("iris");
^TypeError: Cannot read properties of undefined (reading 'get')
at Object.
Clearly, I'm missing something fundamental. How can I call iris
from node.js?
I read the vignettes (especially the Callback to R part) and googled around but couldn't figure this out. Any help would be appreciated!
V8 in R is not connected to nodejs. The idea of that example is you paste that javascript code in the R temrinal, in the ctx$console()
that you just opened.
Or in a scripted way you can do:
library(V8)
ctx <- v8()
# Evaluate some javascript
ctx$eval("var foo = 123 + 456")
ctx$get("foo")
# Call R in JavaScript
script <- 'var iris = console.r.get("iris");
var x = iris[1];
console.log(x)'
ctx$eval(script)
ctx$get("x")
Snap, this is the answer I was afraid of. Could you point to a technique/method/framework that enables to call R from node js?
If you run both on the same machine, you need an npm package that calls R. Maybe this one? https://www.npmjs.com/package/r-integration
If your R is running on a remote server, you could try something like opencpu or similar: https://www.npmjs.com/package/opencpu
Thanks. Yeah I've seen r-integration
but the problem is: calling R from node means it runs Rscript and parses the console print back into node. Which is hacky and prone to error. I'm looking for a more solid solution. v8
indeed seemed too good to be true.