使用终端将参数传递给Rserve
我有一个R代码,它有一个经过训练的机器学习模型。现在我想得到新数据的预测 当前方法:使用终端将参数传递给Rserve,r,rserve,R,Rserve,我有一个R代码,它有一个经过训练的机器学习模型。现在我想得到新数据的预测 当前方法: Script code.R '[{"x1" : "1011", "x2" : "1031", "x4" : "0.65"}]' # Loading the library suppressPackageStartupMessages(library(C50)) suppressPackageStartupMessages(library(jsonlite)) suppressPackageStartupMe
Script code.R '[{"x1" : "1011", "x2" : "1031", "x4" : "0.65"}]'
# Loading the library
suppressPackageStartupMessages(library(C50))
suppressPackageStartupMessages(library(jsonlite))
suppressPackageStartupMessages(library(plyr))
# Loading the trained model
model1 <- readRDS("model1.RDA")
x <- (args[1])
# Function which wraps prediction and son handling
output <- function(x) {
df <- fromJSON(x)
df[is.na(df)] <- 0
prediction <- predict.C5.0(model1, newdata = df, type = "class")
json_df <- toJSON(prediction)
return(json_df)
}
output(x)
var r = require('rserve-client');
r.connect('localhost', 6311, function(err, client) {
client.evaluate('a<-2.7+2', function(err, ans) {
console.log(ans);
client.end();
});
});
我会得到答案,问题是加载和设置环境花费了太多时间
代码:
Script code.R '[{"x1" : "1011", "x2" : "1031", "x4" : "0.65"}]'
# Loading the library
suppressPackageStartupMessages(library(C50))
suppressPackageStartupMessages(library(jsonlite))
suppressPackageStartupMessages(library(plyr))
# Loading the trained model
model1 <- readRDS("model1.RDA")
x <- (args[1])
# Function which wraps prediction and son handling
output <- function(x) {
df <- fromJSON(x)
df[is.na(df)] <- 0
prediction <- predict.C5.0(model1, newdata = df, type = "class")
json_df <- toJSON(prediction)
return(json_df)
}
output(x)
var r = require('rserve-client');
r.connect('localhost', 6311, function(err, client) {
client.evaluate('a<-2.7+2', function(err, ans) {
console.log(ans);
client.end();
});
});
#加载库
SuppressPackageStatupMessages(库(C50))
SuppressPackageStatupMessages(库(jsonlite))
SuppressPackageStatupMessages(库(plyr))
#加载经过训练的模型
model1所以我使用NodeJS与Rserve对话
首先从终端安装节点和npm
npm install --save rserve-client
javascript文件中的代码是:
Script code.R '[{"x1" : "1011", "x2" : "1031", "x4" : "0.65"}]'
# Loading the library
suppressPackageStartupMessages(library(C50))
suppressPackageStartupMessages(library(jsonlite))
suppressPackageStartupMessages(library(plyr))
# Loading the trained model
model1 <- readRDS("model1.RDA")
x <- (args[1])
# Function which wraps prediction and son handling
output <- function(x) {
df <- fromJSON(x)
df[is.na(df)] <- 0
prediction <- predict.C5.0(model1, newdata = df, type = "class")
json_df <- toJSON(prediction)
return(json_df)
}
output(x)
var r = require('rserve-client');
r.connect('localhost', 6311, function(err, client) {
client.evaluate('a<-2.7+2', function(err, ans) {
console.log(ans);
client.end();
});
});
var r=require('rserve-client');
r、 connect('localhost',6311,函数(错误,客户端){
evaluate('a如果你想使用Java
客户端,你需要下载REngine jars
,下面的代码会有帮助(我是从Java
使用的):
您的代码似乎不完整(缺少输出函数的结束括号,并且没有调用输出函数,请编辑您的问题。Thx:-)@RYoda添加了它,谢谢!我认为您基本上必须将代码拆分为客户端脚本和服务器端脚本。服务器端:从您的sorce代码中删除x
赋值和输出
函数调用行,并使用model1.RDA
将此脚本部署到服务器上的文件夹中。客户端:set x variable,RSconnect、 RSserversource,RSassign x,RSeval…(请参阅RSclient
package:的文档了解详细信息)@RYoda谢谢您的帮助,我会检查它的outRserve是基于TCP/IP的,而不是基于HTTP(AFAIK),所以您需要一个客户端库(对不起,我没有使用Rserve w/o R的经验)。请修改您的问题,因为它看起来像是您要使用R客户端。感谢您发布答案。将您的答案标记为已接受完全可以!更多链接:请参阅和