is there anyone, who is working with R and is using java code there? I mean calling Java code from R with "RJAVA" package.
I create my own package and there I have jar file of java code. (like there https://cran.r-project.org/web/packages/helloJavaWorld/vignettes/helloJavaWorld.pdf)
Then I have .r file and I want to call java method. Problem is when I want to return "Java Object of class". There in no problem with "I" as integer or "S" as string. I need Java Object of class in R to continue working with it . It is possible? I found I can return Java Object with return value "L" (e.g."Ljava/lang/Object") but it doesn't work.
This is my R code for calling java code:
FCA <- function(){
a <- .jnew("fcamp/test/MainTest")
b <- .jcall(a, "S", "testFunction")
c <- .jcall(a, "Lfcamp/input/Context;", "testFunction2")
return(c)
}
This is my error:
Error in .jcall(a, "Lfcamp/input/Context;", "testFunction2") :
method testFunction2 with signature ()Lfcamp/input/Context; not found
Where is the mistake ? It is possible return Java Object of class to R and continue to work with it there?
Here is an example (i hope complete) :
I have a java class :
package hello;
public class Hello extends Object {
public String sayHello2(String name) {
String result = new String("Hello " + name);
return result;
}
public Hello sayHello3(String name) {
String result = new String("Hello " + name);
return new Hello();
}
}
sayHello3 returns an Hello object.
To create a jar :
java -cp . hello/Hello.java
jar cvf Hello.jar hello/Hello.class
In my R session : A call with no java reference
library(rJava)
.jinit()
.jaddClassPath(dir( "path to jar", full.names=TRUE ))
.jclassPath() # you should see your jar
hjw <- .jnew("Hello") # create instance of hell/Hello class
outRef <- .jcall(hjw, "S", "sayHello2", "toto", evalString = FALSE)
.jstrVal(outRef)
[1] "Hello World"
And a call to a function returning a java reference:
outRef2 <- .jcall(hjw, "Lhello/Hello;", "sayHello3", "Universe", evalString = T)
.jstrVal(outRef2)
outRef3 <- .jcall(outRef2, "S", "sayHello2", "New Universe", evalString = FALSE)
.jstrVal(outRef3)
returning :
"hello.Hello@74a14482"
"Hello New Universe"