cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar

R-plot though JSL/JMP

I have this script as seen below where I calculate some simple in R, and where I want to see some plots in JSL. I cannot get JSL to show the two plots. When I run the code below it close plot1 and only show plot two. I have tried using what is descirbed here ( https://community.jmp.com/t5/JMPer-Cable/Getting-started-with-the-JMP-to-R-Interface/ba-p/43920 ) but I cannot get it to work. It might be a simple command but I am pretty new to JSL. I hope you can help. FYI I use JMP 17.2 

Names Default To Here(1);

Include("Dialog_script.jsl"); // Load the dialog to get selected X and Y columns
dt = Current Data Table(); // Get the current data table

// Get the selected X and Y columns from the data table
xCol = Column(dt, xColName[1]);
yCol = Column(dt, yColName[1]);

// Check if columns exist
If(xCol == Empty() | yCol == Empty(),
Throw("Error: The selected columns do not exist in the data table.");
);

// Extract the values from xCol and yCol
xValues = xCol << Get Values; // Get the values of xCol
yValues = yCol << Get Values; // Get the values of yCol

// Show the values being sent to R (for debugging)
Show(xValues);
Show(yValues);

// Initialize the R connection
R Init();

// Send the values of xCol and yCol to R as numeric vectors
R Send( xValues );
R Send( yValues );

// Execute the external R script
R Submit File( "C:/Users/NDS/Data_Ana.R" );

 

 

 

// Retrieve the plot 
 R submit( "print(plot1)" );

 

R submit( "print(plot2)" );

 

// Terminate the R connection
//R Term();

1 ACCEPTED SOLUTION

Accepted Solutions

Re: R-plot though JSL/JMP

I believe what you are seeing is plot 2 immediately replaces plot 1 as you have no intervening Wait() or code to capture the image or display the image in a new JMP window.  In running R's plot() you are getting an R window not a JMP window, so when you run the second plot command it's overwriting the first plot.  From the link you referenced, see the section on R Get Graphics().

 

Try something like

...
R Submit("print(plot1)");
New Window("Plot 1", Picture Box( R Get Graphics(png) ) );
R Submit("print(plot2)");
New Window("Plot 2", Picture Box( R Get Graphics(png) ) );
...

Have you looked at the examples in JMP's scripting index, that is located under the Help menu.  The script below comes from the Scripting index 'R Get Graphics'

Names Default To Here( 1 );
R Init();
R Submit( "plot(1:10)" );
plot = R Get Graphics( png );
New Window( "Plot", Picture Box( plot ) );
R Term();

View solution in original post

3 REPLIES 3

Re: R-plot though JSL/JMP

I believe what you are seeing is plot 2 immediately replaces plot 1 as you have no intervening Wait() or code to capture the image or display the image in a new JMP window.  In running R's plot() you are getting an R window not a JMP window, so when you run the second plot command it's overwriting the first plot.  From the link you referenced, see the section on R Get Graphics().

 

Try something like

...
R Submit("print(plot1)");
New Window("Plot 1", Picture Box( R Get Graphics(png) ) );
R Submit("print(plot2)");
New Window("Plot 2", Picture Box( R Get Graphics(png) ) );
...

Have you looked at the examples in JMP's scripting index, that is located under the Help menu.  The script below comes from the Scripting index 'R Get Graphics'

Names Default To Here( 1 );
R Init();
R Submit( "plot(1:10)" );
plot = R Get Graphics( png );
New Window( "Plot", Picture Box( plot ) );
R Term();

Re: R-plot though JSL/JMP

Hi Paul Thank you for your answer. I am trying to repeat the same process just for Python instead of R.

And the JSL keep loading the last defined plot from Python instead of plotting the correct figure.

The plt.show(plt.figure(plot1)) works in Python but when I use the code below in JSL:
plot1 = Picture Box(Python Get Graphics("png","plt.show(plt.figure(plot1)"));

Then JSL keep takeing the last defined plot.

 

Do you have a suggestion of what I wo wrong?

Re: R-plot though JSL/JMP

First, from the documentation in the Scripting Index,  Syntax: image = Python Get Graphics( format );

There is no second parameter.  The scripting index description states: Returns the last graphics object written to the Python graph display window in a graphics format specified by the format parameter.

 

Second, You must be using JMP prior to JMP 18.

Python Get Graphics(png); was deprecated in 18, and already removed from JMP 19 development builds.  In JMP 18 it does not return a value, but instead raises an Alert dialog. The appropriate way which will across all versions of JMP, is to have the Python code save the image to a file. Then Open() the file and hand that off off to a JSL Picture Box().  JMP 18's scripting index for Python Get Graphics() gives a complete example of the workaround.  

Instead of doing a plt.show()

you should do:

# save image to location of your choosing 
plt.savefig( jmp.TEMP + 'get_graphics_img.png') # JMP 18+

# or ... savefig( '/tmp/get_graphics_img.png')  before JMP 18 (macOS).

 Then from JSL:  or via jmp.run_jsl(''' ... '''') from Python 

// plot = Python Get Graphics( png );       // DEPRECATED
pngJMP = New Window( "Plot", Picture Box( Open( "$TEMP/get_graphics_img.png", png ) ) );
Python Submit( "plt.close()" );
rc = Delete File( "$TEMP/get_graphics_img.png" );

There is another issue.  Especially if running on the Mac, matplotlib and JMP fight for control over the window event loop if matplotlib is brought up interactively.  You can get an exit of JMP by closing the last matplotlib window visible on screen.  This depends on the matplotlib backend, but it's especially finicky on macOS.