GUI使用Rstudio打开,但不能从使用Rscript的命令提示符下打开

时间:2019-07-26 10:08:36

标签: r rjava

正在尝试使用Windows10命令提示符中的rjava在Java swing中调用R GUI脚本。当我运行脚本时,它可以在Rstudio中正常运行,但是当我使用R脚本在命令提示符中运行时,GUI窗口不会出现

我用R脚本调用了用Java编写的代码,并且GUI在R studio中打开了,但是在使用Rscript的命令提示符下却不起作用

以下是R代码

library(rJava)
.jinit()
.jclassPath()
x <- .jnew("HelloJava1")
x$test()

以下是Java摇摆代码

import java.awt.*;
import javax.swing.*;

public class HelloJava1 extends JComponent {

public void test() {
        Runnable r = new Runnable() {
        public void run() {
            JFrame f = new JFrame("HelloJava1");
            // f.setSize(300, 300);  better to pack() the frame
            f.getContentPane().add(new HelloJava1());
            // pack should be AFTER components are added..
            f.pack();
            f.setVisible(true);
        }
      };
     // Swing GUIs should be created and updated on the EDT
     SwingUtilities.invokeLater(r);
     }    

@Override  // good practice..
    public void paintComponent(java.awt.Graphics g) {
    // always call super method 1st!
       super.paintComponent(g);
       g.drawString("Hello, Java!", 125, 95);
     }

// instead of setting the size of components, it is 
// better to override the preferred size.
@Override
    public Dimension getPreferredSize() {
      return new Dimension(300,300);
    }
  }

以下是使用命令提示符的代码

D:\Esurveyingsofttech\elevation>"C:\Program Files\R\R-3.6.1\bin\x64\Rscript.exe" D:\\Esurveyingsofttech\\elevation\\TestSwingGui.R

Java swing代码在R studio中打开一个GUI,类似地,当我们使用Rscript运行时,该GUI也应打开。任何帮助都将受到高度赞赏

0 个答案:

没有答案