Bridge Between 'R' and 'Scala' with Callbacks

'Scala' < http://www.scala-lang.org/> is embedded in 'R' and callbacks from 'Scala' to 'R' are available. Support is provided to write 'R' packages that access 'Scala'. After installation, please run 'rscala::scalaConfig()'.


By David B. Dahl (Brigham Young University)

The rscala package provides a bridge between R and Scala, enabling a user to exploit each language's respective strengths in a single project. The rscala package brings Scala and Java libraries into R with a simple interface. Specifically, Scala classes can be instantiated and methods of Scala classes can be called directly. Furthermore, arbitrary Scala code can be executed on the fly from within R and callbacks to R are supported. Conversely, rscala also enables arbitrary R code to be embedded within a Scala application.

Installation

In R, install the package by executing:

install.packages("rscala") 

To install --- or check the compatability of your existing installation of --- Scala and Java, please execute:

rscala::scalaConfig()

Note that if only want to embed R in a Scala application, you don't need to install the package. Simply add the following line to the your SBT build.sbt file:

libraryDependencies += "org.ddahl" %% "rscala" % "3.2.7"

Usage guides

A paper describing the software is "conditionally accepted" and "in editing" at the Journal of Statistical Software. The latest citation information is available using:

citation("rscala")

An updated version of the paper is available here or as a vignette in R (once the package is installed):

vignette("rscala")

The functionality of the software is also described and demonstrated in the help files:

library(help="rscala")
library(rscala)
example(scala)
 

Example packages built with rscala

R extensions can be written using this package, as demonstrated by these packages:

Resources

News

3.2.8 (2019-05-11)

  • Fixed installation problem when Java and/or Scala are not already installed.

3.2.7 (2019-05-11)

  • Added scalaPush and scalaPull to serialize between a list of matrices and Array[Array[Array[Double]].
  • Improved handling of installation of Java, Scala, and SBT.

3.2.6 (2018-12-15)

  • Resolved platform-specific issues arising on Solaris.

3.2.5 (2018-12-07)

  • Avoid unnecessary recompilation in scalaSBT function.
  • Provide RStudio addin to run scalaSBTBuildInstallRestart function.
  • Another way to find Java: Through "R CMD config JAVA".

3.2.4 (2018-11-05)

  • Fixed bug related to garbage collection that caused unexpected crashes.
  • scalaJARs with missing first argument now shows available JAR files.
  • Added READMD.md.
  • Drop deprecated functions scalaPackage and scalaPackageUnload.
  • Don't give up so easily if R cannot immediately read from socket connection.
  • Better setting and restoration of environment variables.
  • Attempt to fix WARN/ERROR for some Windows CRAN machines.

3.2.3 (2018-10-22)

  • Bug fixes in detection of Java version when using early access builds and better treatment of normalizePath.

3.2.2 (2018-10-17)

  • Automatic detection of available RAM and set the Java heap maximum to a reasonable default to avoid 'java.lang.OutOfMemoryError'. Users can still explicitly set it using the argument 'heap.maximum' of the function 'scala' or through the function 'scalaMemory'.
  • Support for downloading and installing Java, Scala, and SBT within R (instead of having the user download, and install a compatible version).
  • Function 'scalaConfig' replaces functions 'scalaInfo' and 'scalaInstall'.
  • Function 'scalaMemory' replaces functions 'scalaHeapMaximum'.
  • Function 'scalaJARs' replaces 'scalaAddJAR'.
  • Added functions 'is.scalaReference', 'scalaFindBridge', 'scalaLazy', 'scalaSBT', 'scalaVersionJARs'.
  • To aid in transferring rich objects between R and Scala, added functions 'scalaPush', 'scalaPull', 'scalaPushRegister' and 'scalaPullRegister'.
  • Support named arguments in transcompiled functions.
  • Removed unnecessary functions 'scalaPackage' and 'scalaPackageUnload'.
  • Added function 'scalaDisconnect' to temporarily disconnect sockets between R and Scala. Sockets are automatically re-established as needed.
  • Support for upcoming release of Scala 2.13.
  • Avoid Window's firewall pop up window.
  • Detection of trying to use a bridge across R instances and more helpful error message.
  • Bug fix regarding callbacks and lexical scoping.

3.2.1 (2018-10-12)

  • Never released.

3.2.0 (2018-10-04)

  • Never released.

3.1.0 (2018-07-12)

  • Preliminary support for transcompiling a (subset of) R code to Scala code.
  • Support for passing arbitrary R objects between R and Scala.
  • Bug fix: Close dangling connection on error from scalaInfo function.

3.0.2 (2018-06-29)

  • Improvements to the API. See descriptions and examples in the help files.
  • Significantly reduced the overhead of making a calls to and from Scala, from milliseconds to hundredths of microseconds.
  • Operator cleanup
    • The + operator replaces the %@% operator.
    • The * operator replaces the %~% and %!% operators and subsequent evaluations skip the compilation step. The * operator is not subject to Scala REPL's memory leak bug. Within a function, arguments are not automatically passed.
    • The ^ operator replaces the %.~% and %.!% operators and is equivalent to the * operator, except it always returns an rscala reference.
  • Arguments are now passed to a Scala snippet by calling the bridge function and using the new * and ^ operators, e.g. s(x=3, y=rnorm(5)) * 'y.map(_+x)'.
  • Overall cleanup of interface.
    • New way to instantiate a class, e.g., name <- s $ .new_String('Alis')
    • New way to get a null reference, e.g., myNull <- s $ .null_String()
    • New way to cast, e.g., name $ '.asInstanceOf[Object]'()
  • Automatic detection of physical RAM and set the Java heap maximum to a reasonable default to avoid 'java.lang.OutOfMemoryError'. Users can still explicitly set it using the argument 'heap.maximum' of the functions 'scala' or 'scalaHeapMaximum'.

2.5.3 (2018-04-10)

  • Resubmitted as per CRAN request.

2.5.2 (2018-04-07)

  • Added support for Java 9, 10, and 11.
  • Removed 'timeout' argument to 'scala' function.

2.5.1 (2018-02-10)

  • Added the .rscalaOptions function to make it easier to set rscala.scala.home, rscala.heap.maximum, rscala.command.line.options.
  • Added 'digest' as a suggested package for building the vignette.
  • Bug fix for rscalaRequire function on Windows.

2.5.0 (2017-11-24)

  • Made 'scala' function return NULL and provide the interpreter as a side effect. This lowers the perceived delay in instantiating and first-use of a Scala interpreter. Added 'scala2' and 'scala3' functions to customize how the Scala/R bridge is established. Users should change instances of 's <- scala()' to 'scala()' or 's <- scala3()'.
  • Caching of methods calls from difference instances of Scala references for faster invocation.
  • Added ability to control which variables are passed to a Scala function using the [ operator. Including additional items can be convenient and excluding items avoids potentially costly serializing over the R/Scala bridge. See the vignette for syntactical details.
  • Added experimental support for data frames with the scalaConvert.data.frame function.
  • Dropped support for Scala 2.10.x and Java 6.

2.4.0 (2017-09-27)

  • Improved input/output performance for large transfers over TCP/IP bridge.
  • Substantially lower protocol overhead when calling Scala methods (e.g., 's$scala.util.Random$nextGaussian()') and Scala functions (e.g., function(x) s %!% '2+x').
  • Scala functions no longer require default arguments and type signatures are now inferred by argument values (instead of being inferred from the default arguments).
  • Fixed broken temperature web app in vignette by replacing defunct 'weatherData' package with 'darksky' package.

2.3.5 (2017-08-30)

  • Quote 'Scala' and 'R' in title and description as per CRAN request.

2.3.4 (2017-08-29)

  • Fixed issue with vignette building on Windows.

2.3.3 (2017-08-28)

  • Make vignette robust to missing microbenchmark package.
  • Make vignette avoid Scala's compiler daemon.

2.3.2 (2017-08-25)

  • Bug fix for depending packages on some platforms.
  • Tweeks to vignette build process to make it more robust.

2.3.1 (2017-08-21)

  • Version associated with resubmission of paper to Journal of Statistical Software.
  • If a requested version of Scala does not exits, offer to install it if R is interactive.
  • By default, set serialize.output to TRUE on Windows and FALSE otherwise so that Windows users also see stack traces and output be default.
  • Added rudimentary support for tab completion for methods on Scala references.
  • RClient is now thread safe.
  • RClient has a 'ping' method to check the vitality of the connection.
  • No longer export 'scalap' function which can be problematic.
  • No longer export the 'scalaOptimize' function.
  • Added the private method 'rscala:::latest()' to install the latest development build from my website.
  • ref$type returns the Scala type of a Scala reference 'ref'.
  • Added s$var(x) to get the value of an R reference 'x'.
  • Bumped default Scala version to 2.12.3.

2.3.0 (2017-08-08)

  • Never released.

2.2.2 (2017-05-24)

  • Improved behavior for packages that depend on rscala, including adding the .rscalaPackageUnload function to spin down the instance of the Scala interpreter/compiler started by the .rscalaPackage function.
  • Moved example to \donttest instead of \dontrun.

2.2.1 (2017-05-24)

  • Never released.

2.2.0 (2017-05-23)

  • Vectors of length one are treated as scalar unless wrapped by the I() function. This new behavior is opposite of the previous behavior but consistent with the jsonlite and RJSONIO packages.
  • Added 'classpath.packages' argument to 'scala' function for easily using JARs for other packages.
  • Added 'rscala.scala.home' global option to provide another means to find the Scala home directory.
  • s$val(), s$.val(), and s$do() are reinstated (but not recommended unless you need the arguments to be variables instead of string literals).
  • Fixed bug by wrapping command.line.arguments by the shQuote function.

2.1.1 (2017-05-11)

  • Added functionality for depending packages to use Scala 2.10.x, 2.11.x, or 2.12.x (rather than what version the developer is using).

2.1.0 (2017-05-08)

  • Scala function defined in R can now use the %!% and %.!% operators. See examples. The alternative s$def() mechanism is deprecated and will be removed.
  • Overhead in Scala functions can be reduced with the scalaOptimize function.
  • s$null() is deprecated in favor of the scalaNull function.
  • s$val() and s$.val() are deprecated in favor of quoted strings, if needed. For example, instead of s$val("_Foo"), use s$"_Foo".
  • For package developers depending on rscala, the embedded interpreter is now only initialized on first use.
  • For package developers depending on rscala, the .rscalaDelayed function is deprecated and will be removed. Instead, use the snippet argument of the .rscalaPackage function.
  • Added support for installing more than just the most recent version of Scala.
  • When attempting to find a suitable Scala installation, look for more than just the most recent version in the "~/.rscala" directory.
  • Internal reduction of duplicate code.

2.0.1 (2017-04-27)

  • Added URL to Scala web page in DESCRIPTION as requested by CRAN.

2.0.0 (2017-04-27)

  • Scala functions defined in R using the s$def() mechanism now specify the arguments using R code instead of a string snippet of Scala code. Protocol overhead is reduced. Function definition is accessible. Caching avoids recompiling the same function for a big speed boost.
  • The s$do() mechanism is deprecated in favor of a simpler syntax. For example, instead of s$do("scala.util.Random")$nextDouble(), use s$.scala.util.Random$nextDouble().
  • Scalars are now supported using the more flexible I() function and the "length.one.as.vector" option is removed.
  • The scalaEval, scalaGet, and scalaSet functions are removed in favor of the %~%, %.~%, %@%, and $ operators.
  • Support for matrices in either row-major or column-major order.
  • Support for byte/raw type.
  • Automatic garbage collection of Scala and R references.
  • RClient now provides invoke methods to simplify calling R functions from Scala and the scalaCallback function is removed.
  • Callbacks to the original R interpreter now properly perform lexical scoping as an R user would expect.
  • Option to set the port numbers for the TCP/IP sockets.
  • Fixed problem that the protocol.R may be inadvertently excluded from the JAR.
  • Other bug fixes, documentation improvements, behind-the-scenes optimization, and simplifications.
  • Bumped default Scala version to 2.12.2.

1.0.15 (2017-02-07)

  • Fixed line ending bug when embedding R in Java/Scala on Windows.
  • Bumped default Scala version to 2.12.1.

1.0.14 (2016-11-21)

  • Reinstated support for Scala 2.10 & 2.12. Supported versions are now 2.10.x, 2.11.x, and 2.12.x.
  • Renamed functions: 1. scalaInterpreter --> scala, 2. rscalaJar --> .rscalaJar, 3. rscalaPackage --> .rscalaPackage
  • Renamed 'intp*' functions to 'scala*' functions, e.g., 'intpSettings' became 'scalaSettings'.
  • Improved ability to find R on Windows using registry keys.
  • Added 'scalaCallback' function to wrap an R function for evaluation by Scala.
  • Improved input/output handling, adding the options serialize, stdout, and stderr options to the 'scala' function.
  • Removed 'intpLoad' function since '.rscalaPackage' provides this functionality.
  • Interrupts in R now close the connection rather than leaving it in an out-of-sync state.
  • Guard elements of the scala interpreter from access by the user through the '$' operator.
  • Fixed bug in return as noted by Duncan Murdoch.

1.0.13 (2016-07-07)

  • Added %@% operator for shorthand notation (e.g, "interpreter %@% snippet") to conveniently evaluate an expression without the overhead involved in returning a result.
  • Added serialize option to scalaInterpreter and intpSettings functions in R to control the capturing of console output. Likewise, added serializeOutput field to the RClient class. Avoiding serialization is faster and leads to a better experence. Dropped the now-redundant quiet option.
  • Added support for 'unlimited' nested callbacks when Scala is embedded in R, where in practice the amount of nesting is only about a dozen if serialize=TRUE (because R runs of out sink resources) but is hundreds of levels if serialize=FALSE.
  • Restructured the package hierarchy, most notably moving RClient to 'org.ddahl.rscala' instead of 'org.ddahl.rscala.callback'.
  • Dropped support for Scala 2.10.
  • Avoid the need to modify the original source of scala.Console object and the need to hijack the boot classpath. The package now uses the standard 'scala' executable from the Scala binary distribution, without any modified code and ugly hacks.

1.0.12 (2016-06-03)

  • Fast instantiation of RClient class (which was slow in 1.0.11).

1.0.11 (2016-05-12)

  • Fixed broken URL.
  • Ship R code in JARs so that rscala does not need to be installed when embedding R in Scala. Add the following to your build.sbt file: libraryDependencies += "org.ddahl" % "rscala_2.11" % "1.0.11"

1.0.10 (2016-05-11)

  • Uploaded JARs to http://central.sonatype.org/
  • Make sure that rscala package is installed when trying to embed R in Scala.
  • Better error messages regarding compatible jars.

1.0.9 (2016-03-11)

  • Bumped support to latest Scala releases (2.11.8 and 2.10.6).
  • Fixed display of README file if no Scala installation is found.

1.0.8 (2015-12-09)

  • Put CRAN URL in canonical form.

1.0.7 (2015-12-09)

  • On Windows, fix problem embedding R within a JVM application.
  • Fixed typographical error in documentation regarding 'strintrplt' function.
  • Fixed bug causing warning 'closing unused connection ...'
  • On Windows, also look at "...Wow6432Node..." registry key.

1.0.6 (2015-05-15)

  • Removed unnecessary 'Suggests' and 'VignetteBuilder'

1.0.5 (2015-05-15)

  • Added README.html to point to paper submitted to Journal of Statistical Software.
  • 'scalaInfo' function is much improved and 'scalaInstall' function is new.
  • Support of arbitrary R objects to be passed as an 'RObject' in Scala.
  • Improved exception handling.
  • Improved support for directly calling methods of an reference, instantiating an object, and calling methods of companion objects.
  • Check that JAR and package version match in JVM-based languages.
  • Added scaladoc for 'RClient' and 'RObject'.
  • Added javadoc for 'RClient'.
  • Documentation clarifications.
  • Minor bug fixes.

1.0.4 (2015-03-24)

  • Modified 'rscalaPackage' and 'rscalaLoad' functions to conform to CRAN policies.
  • Support for subassignment into vectors and lists in RClient.
  • Bug fixes to respect 'java.opts' and 'java.heap.maximum' arguments.

1.0.3 (2015-03-23)

  • Added support to directly calling methods of an reference, instantiating an object, and calling methods of companion objects.
  • Added function 'scalap' to show the names and signatures of class and companion objects.
  • Added methods 'evalD0', 'evalD1', etc. and removed '%~%' method in RClient.
  • Use 'as.reference=NA' in 'intpGet' and friends so that 'as.reference=FALSE' is tried first and, if conversion is not possible, 'as.reference=TRUE' is done.
  • Added 'rscalaPackage' and 'rscalaLoad' functions to facilitate other packages depending on the 'rscala' package.
  • Determine Scala version using Java executable (as located by 'javaCmd' function) instead of Scala executable.
  • Added infrastructure to support alternative interpreters in the future.
  • Miscellaneous bug fixes.

1.0.2 (2015-03-07)

  • On Windows, if all else fails, try to find the Java installation using the Windows registry.
  • Facilitate other packages depending on 'rscala' by explicitly importing 'util' package needed for 'download.file' function.

1.0.1 (2015-03-05)

  • Initial release to CRAN.

1.0.0 (2015-03-03)

  • Release to beta testers.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("rscala")

3.2.15 by David B. Dahl, 12 days ago


https://github.com/dbdahl/rscala


Report a bug at https://github.com/dbdahl/rscala/issues


Browse source code at https://github.com/cran/rscala


Authors: David B. Dahl [aut, cre]


Documentation:   PDF Manual  


Apache License 2.0 | file LICENSE license


Imports utils

Suggests testthat, devtools, rstudioapi, processx

System requirements: Scala (>= 2.11), Java (>= 8)


Imported by bamboo, sdols, shallot.

Suggested by commonsMath.


See at CRAN