我正在运行一个 scala 应用程序,并想使用 Renjin 调用 R 文件并将值从 scala 传递给 R 文件。当我从 scala 加载 R 文件时,找不到 laply 包时出现错误。如果有人能告诉我如何使用 Renjin 将 R 包加载到 scala 中,那就太好了。
下面是我在 scala 中使用 Renjin 调用 R 文件的代码
使用以下命令复制具有依赖项的 jar 文件
scala -cp renjin-script-engine-0.7.0-RC6-jar-with-dependencies.jar
现在 scala 解释器启动了。
导入 javax.script。; 导入 org.renjin.sexp。;
val factory = new ScriptEngineManager();
// 创建一个 R 引擎
val engine = factory.getEngineByName("Renjin");
// 评估磁盘上的 R 脚本
engine.eval(new java.io.FileReader("myscript.R"));
在这一步,它会出错,找不到函数“lapply”
如何将包添加到仁进。我在哪里添加类路径。
下面是R文件的代码
score.sentiment = function (sentences, pos.words,neg.words, .progress='none')
{
require(plyr)
require(stringr)
scores = laply(sentences, function(sentence,pos.words,neg.words){
sentence = gsub('[[:punct:]]','',sentence)
sentence = gsub('[[:cntrl:]]','',sentence)
sentence = gsub('\\d+','',sentence)
sentence = tolower(sentence)
word.list = str_split(sentence, '\\s+')
words = unlist(word.list)
pos.matches = match(words, pos.words)
neg.matches = match(words, neg.words)
pos.matches = !is.na(pos.matches)
neg.matches = !is.na(neg.matches)
score = sum(pos.matches) - sum (neg.matches)
return(score)
},pos.words, neg.words, .progress = .progress)
scores.df = data.frame(score=scores, text=sentences)
return(scores.df)
}
问题的第二部分是如何将参数从 scala 控制台传递到这个 R 文件。
例如,这里的句子是一条推文。我想将它从 scala 发送到 R 函数。