2

我已经在 Windows 7 上成功安装了 JRI 和 rJava。我现在正试图让它在 64 位操作系统的 Ubuntu 上工作。我可以在 R 中进行 rJava 调用,但让 JRI 工作更加困难。我正在运行 NetBeans 7.1.2,并且在设置R_HOMEjava.library.path启用要加载的所有类方面遵循了各种技巧。也就是说,我已经过去了诸如"jri library not found"和之类的错误消息"R_HOME not set"

从我的java代码中,我可以看到R_HOME = /usr/lib64/R

我现在得到的错误信息是

致命错误:您必须指定“--save”、“--no-save”或“--vanilla”

Rengine首次调用时会发生这种情况:

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

这似乎是来自 R 的错误消息;它似乎期待一个命令行参数。我没有看到任何带有此错误消息的帖子。有任何想法吗?谢谢,彼得

4

2 回答 2

3

在此设置中使用 R 需要您在非交互模式下运行 R。要解决此问题,您需要选择错误消息中给出的选项。我会--no-save先试试。这可以防止 R 在运行结束时保存工作区。在 Java 代码中:

String args[] = {"--no-save"};
Rengine re = new Rengine(args, false, null); 
于 2012-09-24T07:48:42.857 回答
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);
    }



    }
于 2012-09-25T15:36:22.400 回答