2012-09-23 4 views
2

Windows 7에 JRI와 rJava를 성공적으로 설치했습니다. 지금은 64 비트 OS로 우분투에서 작동하도록 노력 중입니다. R 내에서 rJava 호출을 할 수 있지만 JRI를 작동시키는 것이 더 어렵습니다. NetBeans 7.1.2를 실행하고 있으며 모든 클래스를로드 할 수 있도록 R_HOMEjava.library.path을 설정하는 다양한 방법을 따랐습니다. 즉, 나는 "jri library not found""R_HOME not set"과 같은 오류 메시지를 지났습니다.우분투에서 JRI 치명적인 오류가 발생했습니다.

내 Java 코드에서 볼 수있는 것은 R_HOME = /usr/lib64/R입니다.

지금 얻을 오류 메시지가이 Rengine가 처음 호출 될 때 발생

Fatal error: you must specify '--save', '--no-save' or '--vanilla'

입니다 :

이 R에서 오류 메시지가 나타납니다

Rengine r = new Rengine(args,false,null);

; 커맨드 라인 인수를 기대하고있는 것 같습니다. 이 오류 메시지가있는 게시를 보지 못했습니다. 어떤 아이디어? 감사합니다, Peter

답변

3

이 설정에서 R을 사용하면 비대화 형 모드로 R을 실행해야합니다. 이 문제를 해결하려면 오류 메시지에 지정된 옵션 중에서 선택해야합니다. 먼저 --no-save을 시도해 보겠습니다. 이렇게하면 R이 실행 종료시 작업 영역을 저장하지 못하게됩니다. Java 코드에서 :

String args[] = {"--no-save"}; 
Rengine re = new Rengine(args, false, null); 
+0

고맙습니다! 그거였다. 이제 작동합니다. –

+0

문제가 해결되면 내 대답에 녹색 체크 표시를 할 수 있습니다. 이것은 당신의 질문에 답이 났음을 분명히합니다. –

0

이 단계를 복제하려는 사람을위한 코드를 게시합니다. 코드는 여러 웹 소스에서 함께 자갈을 긋습니다. 발생하는 다시 포맷하는 것에 대해 유감스럽게 생각합니다. 나는 그것을 직선 텍스트로 표시하는 방법을 모르겠습니다.

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package testjri; 

/** 
* 
* @author root 
*/ 
import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.io.File; 
import java.io.IOException; 
import java.lang.reflect.Field; 
import java.util.Arrays; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import org.rosuda.JRI.Rengine; 
import org.rosuda.JRI.*; 
import org.rosuda.REngine.*; 

public class TestJRI { 

    /** 
    * @param args the command line arguments 
    */ 
public static void main(String[] args) throws IOException 
{//in the folder /etc/ld.so.conf.d I created a file called libR.conf with the single line "/usr/lib64/R/lib/" in it (without the quotes). 
    //In the same folder I created a file called rJava.conf with the single line "/usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server/" in it (without the quotes). 
    //I then ran ldconfig to force these changes. 

    //To get R_HOME set, I had to modify netbeans.conf adding the line "export R_HOME=/usr/lib64/R" 

    System.out.println("R_HOME: "+System.getenv("R_HOME")); 
    try{//This next line is a trick to force a change to java.library.path at runtime 
     addLibraryPath("/usr/lib64/R/site-library/rJava/jri/"); 
     // I copied libR.so to the jri folder so I am not sure if the next line does anything 
     addLibraryPath("/usr/lib64/R/lib/"); 
    }catch(Exception e){System.out.println(e.toString());} 
    System.out.println("java.library.path: "+java.lang.System.getProperty("java.library.path")); 
//Set some labels for the plot 
String title = "R Plot in JFrame"; 
String xlab = "X Label"; 
String ylab = "Y Label"; 
//Start R 
String newargs[] = {"--no-save"}; 

Rengine r = new Rengine(newargs, false, null); 
//Do some calcs and plot the chart but save as a png in the working folder 
r.eval("a<-c(1,2,3,4,5,6,7,8,9,10)"); 
r.eval("b<-c(1,3,2,4,5,6,7,8,9,10)"); 
r.eval("png(file=\"graph2.png\",width=1600,height=1600,res=400)"); 
r.eval("plot(a,b,type='o',col=\"Blue\",main=\"" + title + "\",xlab=\"" 
+ xlab + "\",ylab=\"" + ylab + "\")"); 
r.eval("dev.off()"); 
//It took me a search to find where R stuck the image. I found it in /proc/29285/cwd. 
//I will have to learn how to control the working folder for R from java. 
//get the image and create a new imagepanel 
File file = new File("/proc/29285/cwd/graph2.png"); 
Image image = ImageIO.read(file); 
imagePanel myPanel = new imagePanel(image); 
//Create a new frame and add the imagepanel 
JFrame aFrame = new JFrame(); 
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
aFrame.getContentPane().add(myPanel, BorderLayout.CENTER); 
aFrame.pack(); 
aFrame.setVisible(true); 
aFrame.setSize(new Dimension(600, 600)); 
} 
    static class imagePanel extends JPanel 
    { 
     Image image = null; 

     public imagePanel(Image image) 
     { 
      this.image = image; 
     } 

     @Override 
     public void paintComponent(Graphics g) 
     { 
      super.paintComponent(g); 
      //there is a picture: draw it 
      if (image != null) 
      { 
       int height = this.getSize().height; 
       int width = this.getSize().width; 
       g.drawImage(image, 0, 0, width, height, this); 
      } 
     } 
    } 
    /** 
* Adds the specified path to the java library path 
* 
* @param path the new library path to add 
* @throws Exception 
*/ 
public static void addLibraryPath(String path) throws Exception { 
    String oldPath = System.getProperty("java.library.path"); 
    if (oldPath.length() >0)path = path+":"+oldPath; 
    System.setProperty("java.library.path", path); 

    //set sys_paths to null 
     final Field sysPathsField = ClassLoader.class.getDeclaredField("sys_paths"); 
     sysPathsField.setAccessible(true); 
     sysPathsField.set(null, null); 
    } 



    } 
관련 문제