Domanda

I have successfully installed JRI and rJava on Windows 7. I am now trying to get it to work on Ubuntu with 64bit OS. I can make rJava calls from within R but getting JRI to work is more difficult. I am running NetBeans 7.1.2 and I have followed various tricks in setting R_HOME and java.library.path to enable all the classes to be loaded. That is, I am past the error messages such as "jri library not found" and "R_HOME not set".

From my java code,I can see that R_HOME = /usr/lib64/R.

The error message I get now is

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

This happens when Rengine is first called:

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

This appears to be an error message from R; it seems to be expecting a command line argument. I haven't seen any posting with this error message. Any ideas? Thanks, Peter

È stato utile?

Soluzione

Using R in this setting requires you to run R in non-interactive mode. To solve the issue, you need to choose on of the options given in the error message. I would try --no-save first. This prevents R from saving the workspace at the end of the run. In Java code:

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

Altri suggerimenti

I will post my code for anyone trying to replicate these steps. The code is cobbled together from multiple web sources. Sorry for the re-formatting that occurs: I don't know how to show it as straight text.

/*
 * 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);
    }



    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top