albertosantini / node-rio

Integration with Rserve, a TCP/IP server for R framework
https://github.com/albertosantini/node-conpa
MIT License
176 stars 35 forks source link

Trying to return data, getting error 127 #26

Closed chrislarsenqlik closed 9 years ago

chrislarsenqlik commented 9 years ago

Hi Alberto, sorry in advance for another probably newbish question. I would like to return data back (ultimately in JSON) and am trying to do so in a simple way, which does work in R console but not through Rserve (or node-rio?). Getting an error 127. Similar to before I can get back the state and population in the R console but not

getDataR <- function () {
   require('rjson')
    state <-data.frame(state.x77,region=state.region)
    print(toJSON(state[1]))
    dev.off()
}

Looking at the same FAQ i do see that data is normally returned in binary so you have to use capture.output() to return it, and in the example the FAQ provides it is explicitly casting to string and using a system.out.println() statement. Below is the code without the toJSON() function and using the example. This time I get an "Eval failed with error code 3".

getDataR <- function () {
    state <-data.frame(state.x77,region=state.region)
    String s=c.eval("paste(capture.output(print(state[1])),collapse='\\n')").asString();
    System.out.println(s);
    dev.off()
}

Thanks, Chris

albertosantini commented 9 years ago

You may give a look at the third example (ex3.js, ex3.R).

In R, the functions return the last expression evaluated (dev.off) and I suppose that it is not correct.

chrislarsenqlik commented 9 years ago

Excellent, works like a champ, thanks again!