toJava: Convert R objects to REXP references in Java

Description Usage Arguments Value Examples

View source: R/jri.R

Description

toJava takes an R object and creates a reference to that object in Java. This reference can then be passed to Java methods such that they can refer to it back in R. This is commonly used to pass functions to Java such that Java code can call those functions later.

Usage

1
toJava(x, engine = NULL)

Arguments

x

R object to reference. It can be any R object and it will be retained at least for the duration of the reference on the Java side.

engine

REngine in which the reference is to be created. If <code>null</code> then the last created engine is used. This must be a Java object and a subclass of org.rosuda.REngine (and NOT the old org.rosuda.JRI.Rengine!).

Value

There result is a Java reference (jobjRef) of the Java class REXPReference.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
## Not run: 
  .jinit()
  # requires JRI and REngine classes
  .jengine(TRUE)
  f <- function() { cat("Hello!\n"); 1 }
  fref <- toJava(f)
  # to use this in Java you would use something like:
  # public static REXP call(REXPReference fn) throws REngineException, REXPMismatchException {
  #	 return fn.getEngine().eval(new REXPLanguage(new RList(new REXP[] { fn })), null, false);
  # }
  # .jcall("Call","Lorg/rosuda/REngine/REXP;","call", fref)
  
## End(Not run)

Example output

OpenJDK 64-Bit Server VM warning: Can't detect initial thread stack location - find_vma failed
[1] "Java-Object{Thread[Thread-0,5,main]}"
Warning message:
system call failed: Cannot allocate memory 

rJava documentation built on Dec. 11, 2021, 9:25 a.m.

Related to toJava in rJava...