scala
creates an instance of an embedded Scala
compiler/interpreter and binds a Scala object named R
to permit
callbacks to R. Options are available to customize where Scala is found and
how it is invoked (e.g., setting the classpath and maximum heap size). Multiple
interpreters can be created and each runs independently with its own memory.
Each interpreter can use multiple threads/cores, but the bridge between R and
Scala is not thread-safe. As such, multiple R threads/cores should not
simultaneously access the same interpreter. The functions scalaInfo
and .rscalaJar
provide file
paths to JAR files, installation directories, the Scala executable, and this
package. Note that if you only want to embed R in a Scala application, you do
not need to install the package. Simply add the following line to the your SBT
build.sbt file: libraryDependencies += "org.ddahl"
"_VERSION_", where _VERSION_ is the rscala version number (i.e., 1.0.14).
scalaInstall
downloads and installs Scala in ~/.rscala
in the user's home directory. System administrators should install Scala
globally as described here:
http://www.scala-lang.org/download/install.html. In short, simply
download the archive, unpack it, and add the scala script to the path.
The function .rscalaPackage
should called by the .onLoad
function of a package that wishes to depend on this package. The function
should not be called elsewhere. This function sets the classpath to the JAR
files in the java directory of the package and passes the ...
arguments to the scala
function. This instance of Scala is
available as the object s
in the namespace of the package (thereby
making it available to the package's function) but it is not exported from
the namespace.
scala(classpath=character(0), serialize=FALSE, scala.home=NULL, heap.maximum=NULL, command.line.options=NULL, timeout=60, debug=FALSE, stdout=TRUE, stderr=TRUE)
scalaInfo(scala.home=NULL, verbose=FALSE)
scalaInstall()
.rscalaJar(version="")
.rscalaPackage(pkgname, classpath.appendix=character(0), ...)
FALSE
(the default) is recommended except on Windows where TRUE
may be needed to see output and error messages.NULL
(the default), the function sequentially tries to find the Scala home by: i. using the environment variable SCALA_HOME
, ii. querying the operating system search path, and iii. looking in ~/.rscala/scala-X
where X
is the recommended Scala version number (e.g. 2.11.8) for the package. If all these fail, the function offers to download and install Scala in the above mentioned path (if the current R session is interactive) or it downloads and installs Scala in a temporary directory (if the current R session is noninteractive).NULL
, the global option rscala.heap.maximum
is queried and, if that is also NULL
, Scala's default value is used. This option is ignored if command.line.options is not null.NULL
, the global option rscala.command.line.options
is queried and, if that is also NULL
, the value is set to NULL
. A value of NULL
means no extra arguments are provided. If you simply want to add to the classpath and/or set the maximum heap size, use the classpath and heap.maximum arguments.60
seconds.stdout
and stderr
output that is not serialized is should be sent. TRUE
(the default) or ""
sends output to the R console (although that may not work on Windows). FALSE
or NULL
discards the output. Otherwise, this is the name of the file that receives the output.""
, the package JAR files for all Scala versions are returned. Or, if "2.*.*"
where *
is a placeholder, the JAR file for that specific major version of Scala is returned..onLoad
function) that wishes to depend on this package..rscalaPackage
function. The JAR files in the java directory of the package are already included and do not need to be added here..rscalaPackage
function to the scala
function.scalaEval
,
scalaSet
,
scalaGet
,
scalaDef
,
scalaSettings
,
strintrplt
.rscalaJar()
scalaInfo(verbose=TRUE)
## Not run:
# # Uncomment the next line to download and install Scala
# # scalaInstall()
#
# # Make an instance of the Scala interpreter and see how its output is captured.
# s <- scala(serialize=TRUE)
# capture.output(s %~% 'println("This is Scala "+scala.util.Properties.versionString)')
#
# # Function defined in R
# myMean <- function(x) {
# cat("Here I am in R.\n")
# s %@% 'println("Here I am in Scala.")'
# mean(x)
# }
#
# # Function defined in Scala that calls an R function
# callRFunction <- s$def('functionName: String, x: Array[Double]','
# R.xx = x
# R.evalD0("y <- "+functionName+"(xx)")
# ')
#
# # Have Scala call the R's myMean function with input 1:100.
# callRFunction('myMean',1:100)
# ## End(Not run)
Run the code above in your browser using DataLab