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

How to pass arguments from node to Rserve? #15

Closed liyuqi closed 10 years ago

liyuqi commented 10 years ago

How to pass arguments from node to Rserve, that takes the string from database?

ex:

Rmongo.js

var field1 = req.query.fieldName
rio.sourceAndEval(__dirname + "/test.R", {
    entryPoint: "createDummyPlot",
    param: field1,
    callback: getPlot
});

test.R

xlab1<-list(args[0])
ylab1<-list(args[1])

plot(x,y,xlab=xlab1,ylab=ylab1)

i use Rserve for ploting image...

environment : node - Rserve - mongoDB environment = web - statistic - data

or take a look at https://github.com/kriloBT/nodeSS/tree/master/test_rmongo

albertosantini commented 10 years ago

Hey, I am out of town.

You may give a look at the second example (examples/ex2.js).

Basically you pass a json object and in R script you can extract the params using the RJSONIO package.

The same approach is used for the returned values.

Hope that helps.

albertosantini commented 10 years ago

Any news?

liyuqi commented 10 years ago

thanks alot

i solve it by using example/ex2.js.

the R reads exactly what args pass to

ex: Rmongo.js

var args = {
        db:'rmongodb',      collection:'iris',
        xdata:'Petal.Length',       ydata:'Petal.Width',
        xlab:'Petal.length',        ylab:'petal.width1'
    };

test.R

...
src = fromJSON(data);
cursor <-mongo.find(mongo,paste(src['db'],src['collection'],sep='.'),query='{}');
...
plot(res[,src['xdata']],res[,src['ydata']],xlab=src['xlab'],ylab=src['ylab']);