如何使用节点 js 中的 rserve-js 将参数动态发送到 R 中的 Rserve


var express = require('express');
var router = express.Router();
var session = require('express-session');
let Rserve = require("rserve-js");

var disease="fever";
    var dataz="cough";
enter code here
let client = Rserve.connect("localhost", 6311, function() {
console.log("Connected to Rserve.");
client.eval("datas<-read.csv('~/Desktop/data.csv')", function(err, response) {
    if (err) {
        throw err;
    }
    console.log("table is loaded.");
    client.eval("data<-datas[,c(2,3)]", function(err, response) {
        if (err) {
            throw err;
        }

    });
    client.eval("print(data)", function(err, response) {
        if (err) {
            throw err;
        }

    });

    // client.assign("x", disease);
    // client.assign("y", dataz);
    // client.eval("print(x)");
    client.eval("result<-subset(data,Sym1=='fever' | Sym1=='sweating')", function(err, response) {
        if (err) {
            throw err;
        }

    });
    client.eval("da<-summary(result)", function(err, response) {
        if (err) {
            throw err;
        }
    });
     client.eval("print(da)", function(err, response) {
        if (err) {
            throw err;
        }
        //console.log(response.value); // shows dimension of iris table
    });
      var result= client.eval("summary(result)", function(err, response) {
        if (err) {
            throw err;
        }
        //console.log(response); // shows dimension of iris table
    });
       console.log(result);
});
});

module.exports = router;

我正在发送

Sym1=='fever' 
Sym1=='sweating'

作为用于评估的静态数据,但我希望它是动态的,以便每次Sym1可以不同,有什么方法可以分配变量吗?

它不是很整洁,但你总是可以在 js 中构建子集表达式,如下所示:

var symptoms = ['sweating', 'fever'];
var subsetExpr = symptoms
  .map(function(symptom){ return 'Sym1==' + symptom; })
  .join(' | ');
client.eval("result<-subset(data,"+ subsetExpr +")"

相关内容

  • 没有找到相关文章

最新更新