Search code examples
swingplotjframerjavajri

How to visualize my R plot in my JFrame with JavaGD?


I am new to JRI/rJava/JavaGD and have some problems with it. I drew a simple R plot with JRI and want to include this plot in my customized JFrame. I added the GDCanvas in which the plot should appear to my JFrame. However the plot is not displayed in the GDCanvas, but opens in a new Frame. How can I visualize my R plot in my JFrame, instead of appearing in its own frame?

For me, another possibility would be to modify the new frame in which my plot pops up. But I couldn't add or modify anything there either. Is there a special way to modify frames that appear with JavaGD()?

Can someone please help me? Many thanks in advance.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.rosuda.JRI.Rengine;
import org.rosuda.javaGD.GDCanvas;

public class RjavaGD extends JFrame implements ActionListener {

    private Rengine engine;
    public static GDCanvas gdc;
    private JButton btn;

    public RjavaGD() {
        super();
        super.setTitle("My R Plot");

        btn = new JButton("show plot");
        btn.addActionListener(this);

        gdc = new GDCanvas(400, 400);
        gdc.setBackground(Color.PINK);

        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(btn, BorderLayout.PAGE_START);
        this.getContentPane().add((GDCanvas) gdc, BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);

    // initialize R
        engine = new Rengine(new String[] { "--vanilla" }, false, null);
        engine.eval("library(JavaGD)");
          engine.eval("Sys.putenv('JAVAGD_CLASS_NAME'='RjavaGDInterface')");

    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new RjavaGD();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == btn) {
            engine.eval("JavaGD()");
            engine.eval("a <- c(1,2,3,2,4)");
            engine.eval("plot(a,type=\"l\")");

            gdc.initRefresh();

            engine.end();

            this.setTitle("new random plot");
        }
    }

}


import org.rosuda.javaGD.GDInterface;

public class RjavaGDInterface extends GDInterface {

     public void gdOpen(double w, double h) 
     {
         c = RjavaGD.gdc;        
     }
}

Solution

  • I used Sys.setenv() instead of Sys.putenv(). I also found a way to integrate the plot into my own customized frame, so that I can add buttons and other stuff directly below the plot, which was not possible with the standard frame that opens automatically. This code worked fine for me:

    import org.rosuda.JRI.Rengine;
    import org.rosuda.javaGD.GDCanvas;
    
    public class RjavaGD extends JFrame implements ActionListener {
    
        private Rengine engine;
        private JButton btn;
    
        public RjavaGD() {
            super();
            super.setTitle("My R Plot");
    
            btn = new JButton("show plot");
            btn.addActionListener(this);
            this.getContentPane().setLayout(new BorderLayout());
            this.getContentPane().add(btn, BorderLayout.PAGE_START);
    
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.pack();
            this.setVisible(true);
    
         }
    
        @Override
        public void actionPerformed(ActionEvent e) {
    
            if (e.getSource() == btn) {
            // initialize R
                engine = new Rengine(new String[] { "--vanilla" }, false, null);
                engine.eval("Sys.setenv('JAVAGD_CLASS_NAME'='RjavaGDInterface')");
                engine.eval("library(JavaGD)");
    
                engine.eval("JavaGD()");
                engine.eval("a <- c(1,2,3,2,4)");
                engine.eval("plot(a,type=\"l\")");
    
                engine.end();
            }
        }
    
    }
    
    
    import org.rosuda.javaGD.GDInterface;
    
    public class RjavaGDInterface extends GDInterface {
    
         JFrame f;
    
         @Override
         public void gdOpen(double w, double h) {
            super.gdOpen(w,h);
            f = new JFrame();
            f.setLayout(new BorderLayout());
            c = new GDCanvas(w, h);
    
            f.setTitle("New Plot");
            f.getContentPane().add((GDCanvas) c, BorderLayout.CENTER);
            f.getContentPane().add(buttonPanel(), BorderLayout.NORTH);
    
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setVisible(true);
    
         }
        private JPanel buttonPanel(){
            JPanel p = new JPanel();
            p.setBackground(Color.pink);
    
            p.add(new JLabel("Options“));
            p.add(new JButton("option1“))
    
            return p;
        }
    
    }
    

    I hope this will help other people with the same problem :)