Learn R Programming

rscala (version 1.0.6)

constructor: Creates an instance of an embedded Scala compiler/interpreter. Provides file paths for Java, 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 and Java are found and how they are 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 Rand Scala is not thread-safe. As such, multiple Rthreads/cores should not simultaneously access the same interpreter.

The functions javaCmd, scalaInfo, and rscalaJar provide file paths to JAR files, installation directories, and executables for Java, Scala, 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, java.home=NULL, java.heap.maximum="256M", java.opts=NULL,
    timeout=60, debug.filename=NULL)
javaCmd(java.home=NULL, verbose=FALSE)
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
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 th
java.home
A character vector of length one giving the path where Java is installed. When set to NULL (the default), the function sequentially tries to find the Java home by: i. using the environment variable JAVACMD, ii. using the enviro
java.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 Java with command line argument -Xmx256M. This option is ignored if java.opts
java.opts
A character vector whose elements are passed as command line arguments when invoking the JVM. The default value is NULL, meaning no extra arguments are provided.
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.filename
An option meant only for developers of the package itself and not intended for users of the package.
verbose
A logical vector of length one indicating whether information regarding the search for Scala and Java installation should be displayed.
version
If "", the package JAR files for all Scala versions are returned. Or, if "2.10.*" or "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 Robject representing an embedded Scala interpreter.

    javaCmd returns the path of the Java executable.

    scalaInfo returns a list detailing the Scala executable, version, jars, etc.

See Also

intpEval, intpSet, intpGet, intpDef, intpSettings, strintrplt

Examples

Run this code
rscalaJar()
javaCmd()
scalaInfo(verbose=TRUE)

# Uncomment the next line to download and install Scala
# scalaInstall()

# Make an instance of the Scala interpreter and see how it can print to the R console
s <- scalaInterpreter()
s %~% 'println("This is Scala "+scala.util.Properties.versionString)'

# Function defined in R
myMean <- function(x) {
  cat("Here I am.\n")
  mean(x)
}

# Function defined in Scala that calls an R function
callRFunction <- s$def('functionName: String, x: Array[Double]','
  R.xx = x
  R.eval("y <- "+functionName+"(xx)")
  R.y._1
')

# Have Scala call the R function myMean with input 1:100
callRFunction('myMean',1:100)

Run the code above in your browser using DataLab