Example 2

In this example we get the value of pi and pass it to the binding as a string — if we don’t perform the conversion, we will not see the value when the script is evaluated. What this script demonstrates is that we can pass Java object references into a binding, or text, but we cannot pass references to R variables because they’ll be lost during the evaluation.

library (rJava)
library (rgroovyapi)

localBinding <- .jnew("groovy.lang.Binding")

value <- as.character (pi)

localBinding$setVariable ("pi", value)

# Return is implied.
groovyScript <- "Double.valueOf (pi) * 2"

result <- Evaluate (groovyScript = groovyScript, binding = localBinding)


R Groovy API Example 2