I am running a scala application and would like to call a R file using Renjin and pass values to the R file from scala. When I load the R file from scala i get error on laply package not found. It would be great if some one could tell me how to load R packages into scala using Renjin.
Below is the code i used in scala to call R file using Renjin
Copied the jar file with dependencies using the following command
scala -cp renjin-script-engine-0.7.0-RC6-jar-with-dependencies.jar
Now the scala interpreter started.
import javax.script.; import org.renjin.sexp.;
val factory = new ScriptEngineManager();
// create an R engine
val engine = factory.getEngineByName("Renjin");
// evaluate R script on disk
engine.eval(new java.io.FileReader("myscript.R"));
At this step it error cont not find function 'lapply'
How do i add packages to Renjin. Where do i add the class path.
Below is code for R file
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)
}
Second part of the question is how do I pass parameter from scala console to this R file.
For Example the Sentence here is a tweet. I would like to send it to R function from scala.