我已经成功地在Windows 7上安装了JRI和rJava,现在我正在尝试让它在Ubuntu上使用64位操作系统。我可以从R内部打rJava电话,但是让JRI工作更困难。我正在运行NetBeans 7.1.2,在设置R_HOME和java.library.path以使所有类都能够加载时,我遵循了各种技巧。也就是说,我已经通过了错误消息,如"jri library not found"和"R_HOME not set"。
从我的java代码中,我可以看到R_HOME = /usr/lib64/R。
我现在得到的错误消息是
致命错误:必须指定“--保存”、“不-保存”或“-香草”
当第一次调用Rengine时,就会发生这种情况:
Rengine r=新Rengine(args,false,null);
这似乎是来自R的错误消息;它似乎期待一个命令行参数。我还没有看到任何带有这条错误信息的帖子。有什么想法吗?谢谢,彼得
发布于 2012-09-24 07:48:42
在此设置中使用R需要在非交互模式下运行R。要解决这个问题,您需要选择错误消息中给出的选项。我会先试试--no-save。这使R无法在运行结束时保存工作区。在Java代码中:
String args[] = {"--no-save"};
Rengine re = new Rengine(args, false, null); 发布于 2012-09-25 15:36:22
我将为任何试图复制这些步骤的人发布我的代码。代码是从多个网络来源拼凑而成的。很抱歉发生了重新格式化的情况:我不知道如何将它显示为直接文本。
/*
* 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);
}
}https://stackoverflow.com/questions/12556969
复制相似问题