RMP
2009-Mar-30 05:37 UTC
[R] Problem in S4 object displaying from within a Java application using JRI
I am using JRI (Java R Interface) library in order to call R from within my Java application. But since the "rmu1" and "rmu2" ,see the following code, are objects of type S4 once i run the application the value of Null will be returned for both of them. On this regard, i would appreciate it if anyone can tell me how i am going to display and/ or convert these objects to Java objects or any other way to get the result from R. In order to make the problem more concrete i have attached the entire Java code contain my R program and the R program itself and its actual result run in isolation: Java application: import java.io.*; import java.awt.Frame; import java.awt.FileDialog; import java.util.Enumeration; import org.rosuda.JRI.Rengine; import org.rosuda.JRI.REXP; import org.rosuda.JRI.RList; import org.rosuda.JRI.RVector; import org.rosuda.JRI.RMainLoopCallbacks; public class Main { public static void main(String[] args) { // just making sure we have the right version of everything if (!Rengine.versionCheck()) { System.err.println("** Version mismatch - Java files don't match library version."); System.exit(1); } System.out.println("Creating Rengine (with arguments)"); // 1) we pass the arguments from the command line // 2) we won't use the main loop at first, we'll start it later // (that's the "false" as second argument) // 3) the callbacks are implemented by the TextConsole class above Rengine re = new Rengine(args, false, new TextConsole()); System.out.println("Rengine created, waiting for R"); // the engine creates R is a new thread, so we should wait until it's ready if (!re.waitForR()) { System.out.println("Cannot load R"); return; } try { re.eval("library(kappalab)", true); re.eval("library(methods)", true); re.eval("mu1 <- c(59.73, 27.94,89.67, 62.44)", true); re.eval("mu2<- c(57.81, 72.67, 82.97, 62.44)", true); re.eval("mu3<- c(45.66, 49.19, 53, 64.39)", true); re.eval("mu4<- c(70.39, 80.41, 89.67, 80)", true); re.eval("delta.C <- 1", true); re.eval("Acp <- rbind (c(mu4,mu2,delta.C), c(mu2,mu3,delta.C), c(mu3,mu1,delta.C))", true); re.eval("wt <- c(0.3,0.3,0.25,0.15)/1", true); re.eval("delta.S <- 0.08", true); re.eval("Asp <-rbind ( c(1,4,delta.S), c(2,4,delta.S), c(1,3,delta.S), c(2,3,delta.S),c(3,4,delta.S),c(1,2,-delta.S), c(2,1,-delta.S))", true); re.eval("delta.I <- 0.08", true); re.eval("Aii <- rbind (c(1,2,delta.I,1), c(1,3, delta.I,1), c (1,4, delta.I,1),c(2,3, delta.I,1), c(2,4, delta.I,1), c(3,4,delta.I,1))", true); re.eval("lp <- lin.prog.capa.ident (4, 2, A.Choquet.preorder Acp, A.Shapley.preorder = Asp, A.interaction.interval = Aii)", true); re.eval("mv<- mini.var.capa.ident (4, 2, A.Choquet.preorder Acp, A.Shapley.preorder = Asp, A.interaction.interval = Aii)", true); re.eval("rmu1<- lp$solution", true); re.eval("rmu1", true); re.eval("rmu2<- mv$solution", true); re.eval("rmu2", true); re.eval("rbind(c(mu1,weighted.mean(mu1,wt), Choquet.integral(rmu1,mu1), Choquet.integral(rmu2,mu1)), c(mu2, weighted.mean(mu2,wt), Choquet.integral(rmu1,mu2), Choquet.integral(rmu2,mu2)), c(mu3, weighted.mean(mu3,wt), Choquet.integral(rmu1,mu3), Choquet.integral(rmu2,mu3)),c(mu4, weighted.mean(mu4,wt), Choquet.integral(rmu1,mu4), Choquet.integral(rmu2,mu4)))", true); re.eval("summary (rmu1)", true); re.eval("summary (rmu2)", true); } catch (Exception e) { System.out.println("EX:" + e); e.printStackTrace(); } } } /*-------------------------------------------------------------------------*/ class TextConsole implements RMainLoopCallbacks { public void rWriteConsole(Rengine re, String text, int oType) { System.out.print(text); } public void rBusy(Rengine re, int which) { System.out.println("rBusy(" + which + ")"); } public String rReadConsole(Rengine re, String prompt, int addToHistory) { System.out.print(prompt); try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = br.readLine(); return (s == null || s.length() == 0) ? s : s + "\n"; } catch (Exception e) { System.out.println("jriReadConsole exception: " + e.getMessage()); } return null; } public void rShowMessage(Rengine re, String message) { System.out.println("rShowMessage \"" + message + "\""); } public String rChooseFile(Rengine re, int newFile) { FileDialog fd = new FileDialog(new Frame(), (newFile == 0) ? "Select a file" : "Select a new file", (newFile == 0) ? FileDialog.LOAD : FileDialog.SAVE); fd.show(); String res = null; if (fd.getDirectory() != null) { res = fd.getDirectory(); } if (fd.getFile() != null) { res = (res == null) ? fd.getFile() : (res + fd.getFile()); } return res; } public void rFlushConsole(Rengine re) { } public void rLoadHistory(Rengine re, String filename) { } public void rSaveHistory(Rengine re, String filename) { } } R program embedded in above Java application, but run in isolation:> local({pkg <- select.list(sort(.packages(all.available = TRUE)))+ if(nchar(pkg)) library(pkg, character.only=TRUE)}) Loading required package: lpSolve Loading required package: quadprog Loading required package: kernlab> ## E-MUSEUM 6.11.08 > mu1 <- c(59.73, 27.94, 89.67, 62.44) > mu2<- c(57.81, 72.67, 82.97, 62.44) > mu3<- c(45.66, 49.19, 53, 64.39) > mu4<- c(70.39, 80.41, 89.67, 80) > > ##preference threshold relative > ## to the preorder of the alternatives > delta.C <-1 > > ##corresponding Choquet preorder constraint matrix > Acp <- rbind (c(mu4,mu2,delta.C), c(mu2,mu3,delta.C), c(mu3,mu1,delta.C)) > > > ## weight 1=0.3, 2=0.3, 3=0.25, 4=0.15 > ## Shapley preorder constraint matrix > ## criteria 1,2 should have the same global importance > ## criteria 3 is more important than criteria 4 > > wt <- c(0.3,0.3,0.25,0.15)/1 > delta.S <- 0.08 > > Asp <-rbind ( c(1,4,delta.S), c(2,4,delta.S), c(1,3,delta.S), > c(2,3,delta.S),c(3,4,delta.S),c(1,2,-delta.S), c(2,1,-delta.S)) > > ## an interaction preorder constraint matrix > delta.I <- 0.08 > Aii <- rbind (c(1,2,delta.I,1), c(1,3, delta.I,1), c (1,4, delta.I,1),+ + c(2,3, delta.I,1), c(2,4, delta.I,1), c(3,4,delta.I,1))> > lp <- lin.prog.capa.ident (4, 2, A.Choquet.preorder = Acp, > A.Shapley.preorder = Asp, A.interaction.interval = Aii)Success: the objective function is 0.0239992> > > mv<- mini.var.capa.ident (4, 2, A.Choquet.preorder = Acp, > A.Shapley.preorder = Asp, A.interaction.interval = Aii) > > rmu1<- lp$solution > rmu1Mobius.capacity {} 0.000000 {1} 0.000001 {2} 0.000001 {3} 0.000001 {4} 0.000001 {1,2} 0.391998 {1,3} 0.183999 {1,4} 0.080000 {2,3} 0.183999 {2,4} 0.080000 {3,4} 0.080000> > rmu2<- mv$solution > unclass(rmu2)<S4 Type Object> attr(,"subsets") [1] 0 1 2 4 8 3 5 9 6 10 12 attr(,"k") [1] 2 attr(,"data") [1] 0.00000000 0.18284810 0.19427815 0.08567053 0.02284810 0.08000000 [7] 0.08000000 0.08000000 0.11435513 0.08000000 0.08000000 attr(,"n") [1] 4> rmu2Mobius.capacity {} 0.000000 {1} 0.182848 {2} 0.194278 {3} 0.085671 {4} 0.022848 {1,2} 0.080000 {1,3} 0.080000 {1,4} 0.080000 {2,3} 0.114355 {2,4} 0.080000 {3,4} 0.080000> > rbind(c(mu1,weighted.mean(mu1,wt), Choquet.integral(rmu1,mu1), > Choquet.integral(rmu2,mu1)), c(mu2, weighted.mean(mu2,wt), > Choquet.integral(rmu1,mu2), Choquet.integral(rmu2,mu2)), c(mu3, > weighted.mean(mu3,wt), Choquet.integral(rmu1,mu3), > Choquet.integral(rmu2,mu3)),+ c(mu4, weighted.mean(mu4,wt), Choquet.integral(rmu1,mu4), Choquet.integral(rmu2,mu4))) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 59.73 27.94 89.67 62.44 58.0845 39.09266 47.67584 [2,] 57.81 72.67 82.97 62.44 69.2525 61.28507 65.39835 [3,] 45.66 49.19 53.00 64.39 51.3635 47.17915 48.67584 [4,] 70.39 80.41 89.67 80.00 79.6575 73.77131 76.89140> > summary (rmu1)Shapley value : 1 2 3 4 0.3279994 0.3279994 0.2240002 0.1200010 Shapley interaction indices : 1 2 3 4 1 NA 0.3919976 0.1839992 0.08 2 0.3919976 NA 0.1839992 0.08 3 0.1839992 0.1839992 NA 0.08 4 0.0800000 0.0800000 0.0800000 NA Orness : 0.2222233 Veto indices : 1 2 3 4 0.8471095 0.8471095 0.7546657 0.6622220 Favor indices : 1 2 3 4 0.2568897 0.2568897 0.2106679 0.1644460 Normalized variance : 0.2709092 Normalized entropy : 0.6586446> summary (rmu2)Shapley value : 1 2 3 4 0.3028481 0.3314557 0.2228481 0.1428481 Shapley interaction indices : 1 2 3 4 1 NA 0.0800000 0.0800000 0.08 2 0.08 NA 0.1143551 0.08 3 0.08 0.1143551 NA 0.08 4 0.08 0.0800000 0.0800000 NA Orness : 0.3571236 Veto indices : 1 2 3 4 0.6761999 0.6990889 0.6266838 0.5695332 Favor indices : 1 2 3 4 0.3942643 0.4095188 0.3371137 0.2875976 Normalized variance : 0.07842338 Normalized entropy : 0.9035183>-- View this message in context: http://www.nabble.com/Problem-in-S4-object-displaying-from-within-a-Java-application-using-JRI-tp22776672p22776672.html Sent from the R help mailing list archive at Nabble.com.