您对
的可能性有所了解吗?祝你好运
拉斐尔
答案 0 :(得分:21)
查看CRAN上可用的R中的jvmr package。它允许您:
它还允许您对Java执行相同的操作。描述其用法的文章是here。 (披露:我是作者。)
答案 1 :(得分:15)
CRAN上有一个R包用于此目的,称为" rscala"。它允许双向调用(来自R的Sc和来自R的Scala),以及回调(例如,从R调用的Scala代码调用R)。它是well documented。这个包取代了" jvmr"另一个答案中提到的包。
答案 2 :(得分:4)
我不知道是否有直接的Scala接口,但rJava http://www.rforge.net/rJava/应该有帮助。
答案 3 :(得分:4)
我能够使用jvmr实现它。下面的代码是我从scala控制台运行的示例apache spark应用程序。
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"))
}
}
希望这会有所帮助。
答案 4 :(得分:3)
可以使用rJava,但我不相信这是最好的方法。
答案 5 :(得分:2)
https://github.com/hughleat/scala2R
我在学习Scala的同时写了这篇文章。不确定它是否有效。这是一个包装JRI的小DSL。可能macros等人现在可以做得更好。
答案 6 :(得分:1)