Learn R Programming

rscala (version 1.0.13)

constructor: Creates an instance of an embedded Scala compiler/interpreter. Provides file path for Scala and this package's JAR file. Provides functions to enable other packages to depend on this package.

Description

The function scalaInterpreter 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. 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 only be called by the .onLoad function of a package that wishes to depend on this package. The function creates an environment E in that package's namespace which is available in any of that package's functions but is not exported. The E$jars variable is a character vector giving the full path of all the JAR files in the ‘java’ directory of that package. The E$pkgname variable gives that package's name. Once the rscalaLoad function is called (probably outside that packages's .onLoad function), E$s is an instance of an embedded Scala compiler/interpreter. Like rscalaPackage, rscalaLoad should only be called by a package that wishes to depend on this package.

Usage

scalaInterpreter(classpath=character(0), scala.home=NULL, heap.maximum="256M", command.line.options=NULL, timeout=60, debug=FALSE, serialize=.Platform$OS.type == "windows") scalaInfo(scala.home=NULL, verbose=FALSE) scalaInstall() rscalaJar(version="") rscalaPackage(pkgname) rscalaLoad(classpath=NULL, ...)

Arguments

classpath
A character vector whose elements are paths to JAR files or directories which specify the classpath for the Scala compiler/interpreter. If left equal to NULL in the function rscalaLoad, the package variable E$jars --- which is set by an earlier call to the function rscalaPackage --- is used.
scala.home
A character vector of length one giving the path where Scala is installed. When set to 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.5) 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).
heap.maximum
A character vector of length one used to specify the maximum heap size in the JVM. The default is ‘256M’ which is equivalent to invoking Scala with command line argument ‘-J-Xmx256M’. This option is ignored if command.line.options is not null.
command.line.options
A character vector whose elements are passed as command line arguments when invoking Scala. The default value is NULL, meaning 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.
timeout
A numeric vector of length one giving the number of seconds to wait for Scala to start before aborting. The default value is 60 seconds.
debug
An option meant only for developers of the package itself and not intended for users of the package.
serialize
An logical vector of length one indicating whether standard output (stdout) and standard error (stderr) should be captured and serialized back to R. For performance and a better experience, FALSE is the default on non-Windows operating systems and the operating system will take care of displaying this output. Due to limitations of Windows, TRUE is the default value on Windows operating systems as the output may not be displayed otherwise. If this output is not needed, FALSE is recommended.
verbose
A logical vector of length one indicating whether information regarding the search for the Scala installation should be displayed.
version
If "", the package JAR files for all Scala versions are returned. Or, if "2.11.*" where * is a placeholder, the JAR file for that specific major version of Scala is returned.
pkgname
A character string giving the name of the package (as provided the second argument of the .onLoad function) that wishes to depend on this package.
...
These arguments are passed to the scalaInterpreter function.

Value

scalaInterpreter returns an R object representing an embedded Scala interpreter.scalaInfo returns a list detailing the Scala executable, version, jars, etc.

See Also

intpEval, intpSet, intpGet, intpDef, intpSettings, strintrplt

Examples

Run this code
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 <- scalaInterpreter(serialize=TRUE)
# 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