我正在运行scala应用程序,并希望使用Renjin调用一个R文件,并将值从scala传递给R文件。当我从scala加载R文件时,没有找到laply包上的错误。如果有人能告诉我如何使用Renjin将R包加载到scala中,那就太好了。
下面是我在scala中使用Renjin调用R文件的代码
//创建一个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函数。
发布于 2014-04-30 10:33:06
我不相信普莱尔或弦乐会和人进合作。我还没有检查过,但我认为plyr在GNU R的C Api中有相当大的魔力,Renjin似乎在一些stringr的测试函数上使用了卡箍。
但是,我不认为您需要上述函数中的任何一个包,只需将laply和str_split分别从基本包中替换为sapply和str拆分。
像上面这样评估了函数定义之后,就可以使用invokeFunction方法从Scala/Java调用这个函数:
((Invocable)engine).invokeFunction("score.sentiment",
"Best pizza EVER!",
new String[] { "best", "cool" },
new String[] { "sucks", "awful" });
Renjin将将字符串数组转换为StringVector对象(R字符对象),但您也可以自己创建StringVector对象。
http://docs.oracle.com/javase/7/docs/api/javax/script/Invocable.html#invokeFunction(java.lang.String
发布于 2014-07-03 18:56:05
我能够使用jvmr在scala中使用r包。下面是一个示例代码。
package org.scala.rtest
import org.ddahl.jvmr.RInScala
object RIntegration {
def main(args: Array[String]) {
val R = RInScala()
R>"""
require(sparkR)
score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
require(plyr)
require(stringr)
scores = laply(sentences, function(sentence, pos.words, neg.words) {
# clean up sentences with R's regex-driven global substitute, gsub():
sentence = gsub('[[:punct:]]', '', sentence, ignore.case=T)
sentence = gsub('[[:cntrl:]]', '', sentence, ignore.case=T)
sentence = gsub('\\d+', '', sentence, ignore.case=T)
# and convert to lower case:
sentence = tolower(sentence)
# split into words. str_split is in the stringr package
word.list = str_split(sentence, '\\s+')
# sometimes a list() is one level of hierarchy too much
words = unlist(word.list)
# compare our words to the dictionaries of positive & negative terms
pos.matches = match(words, pos.words)
neg.matches = match(words, neg.words)
# match() returns the position of the matched term or NA
# we just want a TRUE/FALSE:
pos.matches = !is.na(pos.matches)
neg.matches = !is.na(neg.matches)
# and conveniently enough, TRUE/FALSE will be treated as 1/0 by sum():
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)
}
"""
R(" x <- scan('positive-words.txt',what='character',comment.char=';')")
R(" y <- scan('negative-words.txt',what='character',comment.char=';')")
R(" z <- scan('twitterstream1.txt', what='character' )")
R.eval("df <- score.sentiment(z,x,y)")
println(R.capture("df"))
}
}
希望这能帮上忙。
发布于 2016-05-11 06:27:55
爪哇:
构建专为renjin编译的包的版本。然后使用maven或其他构建工具将其添加到类路径中。
欲了解更多信息,请参阅人进文档介绍:
http://docs.renjin.org/en/latest/introduction.html#using-cran-packages-in-renjin
https://stackoverflow.com/questions/22951471
复制相似问题