【问题标题】:JRI and R - not able to capture outputJRI 和 R - 无法捕获输出
【发布时间】:2011-12-14 09:23:57
【问题描述】:

我正在尝试使用 Java 和 R(JRI 和 Rengine)绘制一些图表。当我在我的代码中写这样的东西时,它工作正常:

re.eval("plot(c(1,5,3,8,5), type='l', col=2)");

但如果我这样写:

re.eval("source(\"C:\\Documents and Settings\\abc\\My Documents\\Test Data\\BoxPlot.r\");");

输出窗口闪烁片刻然后消失。此指定文件仅包含 1 个命令,作为第一个,即 re.eval("plot(c(1,5,3,8,5), type='l', col=2)");

我对 R 很陌生,所以这可能是一个非常基本的问题。但我无法找到它。谁能帮帮我?

谢谢。

编辑 1

我使用的是 Windows XP。

这里是完整的代码:

import org.rosuda.JRI.Rengine;

public class JavaGDExample1 {

  public static void main(String[] args) {
    Rengine re;
    String[] dummyArgs = new String[1];
    dummyArgs[0] = "--vanilla";
    re = new Rengine(dummyArgs, false, null);
    re.eval("library(JavaGD)");

    // This is the critical line: Here, we tell R that the JavaGD() device that
    // it is supposed to draw to is implemented in the class MyJavaGD. If it were
    // in a package (say, my.package), this should be set to
    // my/package/MyJavaGD1.
    re.eval("Sys.putenv('JAVAGD_CLASS_NAME'='MyJavaGD1')");

    re.eval("JavaGD()");
//    re.eval("plot(c(1,5,3,8,5), type='l', col=2)");
    re.eval("source(\"C:\\Documents and Settings\\abc\\My Documents\\Test Data\\BoxPlot.r\");");
//    re.eval("source(\"C:\\Documents and Settings\\abc\\My Documents\\Test Data\\testPlot.r\")");
    re.end();
  }
}


import javax.swing.JFrame;

import org.rosuda.javaGD.GDCanvas;
import org.rosuda.javaGD.GDInterface;

/**
 * This is a minimal reimplementation of the GDInterface. When the device is opened, 
 * it just creates a new JFrame, adds a new GDCanvas to it (R will plot to this GDCanvas)
 * and tells the program to exit when it is closed.   
 */
public class MyJavaGD1 extends GDInterface {
  public JFrame f;

  public void gdOpen(double w, double h) {
    f = new JFrame("JavaGD");
    c = new GDCanvas(w, h);
    f.add((GDCanvas) c);
    f.pack();
    f.setVisible(true);
    f.setTitle("Naked R plot");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

}

【问题讨论】:

    标签: java r plot jri


    【解决方案1】:

    re.eval("Sys.putenv('JAVAGD_CLASS_NAME'='MyJavaGD1')");

    应该是现在

    re.eval("Sys.Sys.setenv('JAVAGD_CLASS_NAME'='MyJavaGD1')");

    Sys.setenv() 是 Sys.putenv() 的新(和首选)同义词,现在已弃用,取而代之的是 Sys.setenv()。

    source

    【讨论】:

      【解决方案2】:

      你的路径是错误的 - 你正在进入报价地狱,你发送给 R 的是

       source("C:\Documents and Settings\abc\My Documents\Test Data\BoxPlot.r");
      

      这是无效的:

       > source("C:\Documents and Settings\abc\My Documents\Test Data\BoxPlot.r");
       Error: '\D' is an unrecognized escape in character string starting "C:\D"
      

      为避免引用地狱,分配文件名更安全:

       re.assign("fn", "c:\\foo\\bar");
       re.eval("source(fn)");
      

      或使用/ 代替\\

      PS:使用 stats-rosuda-devel 解决 rJava/JRI 问题

      【讨论】:

      • 我不得不修改`re.eval("source(\"C:\\Documents and Settings\\abc\\My Documents\\Test Data\\BoxPlot.r\"); ");` as ` re.eval("source(\"C:\\\\Documents and Settings\\\\bchitte\\\\My Documents\\\\Vistakon\\\\Test Data\\\\ testPlot.r\")");`
      • 基本上我添加到添加 4 ``而不是 2。你的回答给了我正确的方向。谢谢。
      • 但它仍然给我留下了 1 个新问题。如果初始路径本身是错误的,R 如何显示瞬间消失的窗口?
      • 该窗口来自您直接评估的JavaGD() 调用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 2014-04-21
      • 2014-12-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      相关资源
      最近更新 更多