从 Scala 使用 R 并从 R 调用 Scala?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4914526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using R from Scala and invoking Scala from R?
提问by Raffael
Do you know something about possibilites of
你知道一些关于可能性的事情吗?
- invoking Scala from R and
- using R (libraries) from within Scala?
- 从 R 调用 Scala 和
- 在 Scala 中使用 R(库)?
Best regards
最好的祝福
Raffael
拉斐尔
采纳答案by Matti Pastell
I don't know if there is direct Scala interface, but rJava http://www.rforge.net/rJava/should help.
我不知道是否有直接的 Scala 接口,但是 rJava http://www.rforge.net/rJava/应该会有所帮助。
回答by David B. Dahl
Check out the jvmr packagein R available on CRAN. It allows you to:
查看CRAN 上可用的 R 中的jvmr 包。它允许您:
- embed the R interpreter in Scala
- embed the Scala interpreter/compiler in R.
- 在 Scala 中嵌入 R 解释器
- 在 R 中嵌入 Scala 解释器/编译器。
It also allows you to do the same with Java. An article describing its usage is here. (Disclosure: I'm the author.)
它还允许您对 Java 执行相同的操作。描述其用法的文章是here。(披露:我是作者。)
回答by Darren Wilkinson
There is an R package on CRAN for exactly this purpose, called "rscala". It allows bi-directional calling (R from Scala and Scala from R), as well as callbacks (eg. calling back to R from Scala code called from R). It is well documented. This package replaces the "jvmr" package mentioned in another answer.
CRAN 上有一个 R 包正是为此目的,称为“rscala”。它允许双向调用(来自 Scala 的 R 和来自 R 的 Scala),以及回调(例如,从 R 调用的 Scala 代码回调到 R)。这是有据可查的。这个包替换了另一个答案中提到的“jvmr”包。
回答by Pawan
I was able to achieve it using jvmr. The code below is sample apache spark application i am running from scala console.
我能够使用 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"))
}
}
Hope this helps.
希望这可以帮助。
回答by Rex Kerr
回答by hughleat
https://github.com/hughleat/scala2R
https://github.com/hughleat/scala2R
I wrote this while learning Scala. Not sure if it works anymore. It was a little DSL wrapping JRI. Probably macros et al could do a lot better now.
我在学习 Scala 时写了这个。不确定它是否有效。这是一个包装 JRI 的小 DSL。可能宏等人现在可以做得更好。

