Yes, that is possible.
Note that by calling engine.eval()
on the original ScriptEngine instance, the expression will be evaluated in the global environment and the R function won't be able to see the R call stack that invoked the Java method.
You can also ask Renjin to pass the current Context
to your Java method when invoked. For example:
class MyJavaClass {
static SEXP estimate(@Current Context context, SEXP function) {
return context.evaluate(FunctionCall.newCall(function, IntVector.valueOf(42)));
}
}
And then:
import(MyJavaClass)
f <- function(x) x*2
MyJavaClass$estimate(f) # 84