Description Details Author(s) See Also Examples
The jsr223 package provides a high-level integration for Java that makes Java objects easy to use from within R and simplifies bi-directional data exchange for a wide variety of objects. Furthermore, jsr223 employs the Java Scripting API to bring several scripting languages to the R software environment: JavaScript, Ruby, Python, Groovy, and Kotlin.
The complete documentation is in the jsr223 User Manual. It includes in-depth code examples and it covers details, such as data exchange, that cannot be addressed easily in the R documentation.
Floid R. Gilbert floid.r.gilbert@gmail.com, David B. Dahl dahl@stat.byu.edu
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | # Simple example embedding JavaScript.
library("jsr223")
engine <- ScriptEngine$new("javascript")
engine$radius <- 4
engine %~% "var area = Math.PI * Math.pow(radius, 2)"
cat ("The area of the circle is ", engine$area, ".\n", sep = "")
# Use callbacks to set values, get values, and execute R code
# in the current R session via the global R object.
# Access R from JavaScript.
engine %@% "R.set('a', 12);"
engine %@% "print(\"The value of 'a' is \" + R.get('a') + \".\");"
engine %@% "var randomNormal = R.eval('rnorm(5)');"
engine$randomNormal
# Use a Java object.
engine$randomNormal <- rnorm(5)
engine$randomNormal
engine %@% "java.util.Arrays.sort(randomNormal)"
engine$randomNormal
# Close the engine and release resources.
engine$terminate()
|
OpenJDK 64-Bit Server VM warning: Can't detect primordial thread stack location - find_vma failed
Warning: Nashorn engine is planned to be removed from a future JDK release
The area of the circle is 50.26548.
The value of 'a' is 12.
[1] 0.8325135 0.6401375 1.2177305 0.8519089 -0.9333444
[1] -1.44079852 0.89656714 -0.44837679 -0.51116916 0.01831914
[1] -1.44079852 -0.51116916 -0.44837679 0.01831914 0.89656714
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.