2010-10-09 82 views
4

嗨,大家好 我想問一下,是否有人成功與JRI和rJava合作?我想把一些圖形,R中的圖形放到我的Java應用程序中,但沒有成功。任何人都可以提供工作示例。這是我發現的,但它不起作用。謝謝從R調用到Java應用程序

import org.rosuda.JRI.REXP; 
import org.rosuda.JRI.Rengine; 
/** 
* @author Nero 
*In this file, i will try to plot a simple example, only to test how it?s possible to plot through java 
*Attention: Nothing will work if you have not included the JRI.jar as library (through properties)*/ 

public class TryPlot { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 

     //start the Rengine (JRI) 
     Rengine re = new Rengine(null, false, null); 

     //in R: >a<- c(1.2,2.3,4.5) : 
     double da[] = {1.2, 2.3, 4.5}; 
     long xp3 = re.rniPutDoubleArray(da); 
     re.rniAssign("a", xp3, 0); 
     //look up for a: 
     REXP x; 
     x = re.eval("a"); 
     System.out.println(x); 
     //THE PROBLEM: The window opens, but nothing happens??? 
     re.eval(" plot(a)"); 
    } 

} 

回答

2

我認爲普通的R圖形設備只有在R GUI中使用它時才起作用,而不是從java或命令行啓動。 因此,我使用包「JavaGD」作爲圖形設備,這工作正常。 Plot打印在一個普通的JFrame中,甚至可以通過子類化來擴展它。