Skip to content

Instantly share code, notes, and snippets.

@darrenjw
Created August 15, 2015 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenjw/c123cb111160ba61b27e to your computer and use it in GitHub Desktop.
Save darrenjw/c123cb111160ba61b27e to your computer and use it in GitHub Desktop.
R function using "rscala" and SBT to allow the inlining of Scala Breeze code in R
#> b=breezeInterpreter()
#> b%~%'import breeze.stats.distributions._'
#ScalaInterpreterReference... engine: javax.script.ScriptEngine
#> b%~%'Poisson(10).sample(20).toArray'
# [1] 10 12 15 11 11 8 12 6 10 10 11 11 17 5 13 8 7 15 11 11
#> summary(b%~%'Gamma(3,2).sample(10000).toArray')
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 0.2244 3.4770 5.3070 5.9590 7.7770 25.8700
breezeInterpreter<-function()
{
library(rscala)
sbtStr="name := \"tmp\"
version := \"0.1\"
libraryDependencies ++= Seq(
\"org.scalanlp\" %% \"breeze\" % \"0.10\",
\"org.scalanlp\" %% \"breeze-natives\" % \"0.10\"
)
resolvers ++= Seq(
\"Sonatype Snapshots\" at \"https://oss.sonatype.org/content/repositories/snapshots/\",
\"Sonatype Releases\" at \"https://oss.sonatype.org/content/repositories/releases/\"
)
scalaVersion := \"2.11.6\"
lazy val printClasspath = taskKey[Unit](\"Dump classpath\")
printClasspath := {
(fullClasspath in Runtime value) foreach {
e => print(e.data+\"!\")
}
}
"
tmps=file(file.path(tempdir(),"build.sbt"),"w")
cat(sbtStr,file=tmps)
close(tmps)
owd=getwd()
setwd(tempdir())
cpstr=system2("sbt","printClasspath",stdout=TRUE)
cpst=cpstr[length(cpstr)]
cpsp=strsplit(cpst,"!")[[1]]
cp=cpsp[2:(length(cpsp)-1)]
si=scalaInterpreter(cp)
setwd(owd)
si
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment