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
%3CLINGO-SUB%20id%3D%22lingo-sub-803149%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ER-plot%20though%20JSL%2FJMP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-803149%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EI%20have%20this%20script%20as%20seen%20below%20where%20I%20calculate%20some%20simple%20in%20R%2C%20and%20where%20I%20want%20to%20see%20some%20plots%20in%20JSL.%20I%20cannot%20get%20JSL%20to%20show%20the%20two%20plots.%20When%20I%20run%20the%20code%20below%20it%20close%20plot1%20and%20only%20show%20plot%20two.%20I%20have%20tried%20using%20what%20is%20descirbed%20here%20(%20%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FJMPer-Cable%2FGetting-started-with-the-JMP-to-R-Interface%2Fba-p%2F43920%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fcommunity.jmp.com%2Ft5%2FJMPer-Cable%2FGetting-started-with-the-JMP-to-R-Interface%2Fba-p%2F43920%26nbsp%3B%3C%2FA%3E)%20but%20I%20cannot%20get%20it%20to%20work.%20It%20might%20be%20a%20simple%20command%20but%20I%20am%20pretty%20new%20to%20JSL.%20I%20hope%20you%20can%20help.%20FYI%20I%20use%20JMP%2017.2%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3ENames%20Default%20To%20Here(1)%3B%3C%2FP%3E%3CP%3EInclude(%22Dialog_script.jsl%22)%3B%20%2F%2F%20Load%20the%20dialog%20to%20get%20selected%20X%20and%20Y%20columns%3CBR%20%2F%3Edt%20%3D%20Current%20Data%20Table()%3B%20%2F%2F%20Get%20the%20current%20data%20table%3C%2FP%3E%3CP%3E%2F%2F%20Get%20the%20selected%20X%20and%20Y%20columns%20from%20the%20data%20table%3CBR%20%2F%3ExCol%20%3D%20Column(dt%2C%20xColName%5B1%5D)%3B%3CBR%20%2F%3EyCol%20%3D%20Column(dt%2C%20yColName%5B1%5D)%3B%3C%2FP%3E%3CP%3E%2F%2F%20Check%20if%20columns%20exist%3CBR%20%2F%3EIf(xCol%20%3D%3D%20Empty()%20%7C%20yCol%20%3D%3D%20Empty()%2C%3CBR%20%2F%3EThrow(%22Error%3A%20The%20selected%20columns%20do%20not%20exist%20in%20the%20data%20table.%22)%3B%3CBR%20%2F%3E)%3B%3C%2FP%3E%3CP%3E%2F%2F%20Extract%20the%20values%20from%20xCol%20and%20yCol%3CBR%20%2F%3ExValues%20%3D%20xCol%20%26lt%3B%26lt%3B%20Get%20Values%3B%20%2F%2F%20Get%20the%20values%20of%20xCol%3CBR%20%2F%3EyValues%20%3D%20yCol%20%26lt%3B%26lt%3B%20Get%20Values%3B%20%2F%2F%20Get%20the%20values%20of%20yCol%3C%2FP%3E%3CP%3E%2F%2F%20Show%20the%20values%20being%20sent%20to%20R%20(for%20debugging)%3CBR%20%2F%3EShow(xValues)%3B%3CBR%20%2F%3EShow(yValues)%3B%3C%2FP%3E%3CP%3E%2F%2F%20Initialize%20the%20R%20connection%3CBR%20%2F%3ER%20Init()%3B%3C%2FP%3E%3CP%3E%2F%2F%20Send%20the%20values%20of%20xCol%20and%20yCol%20to%20R%20as%20numeric%20vectors%3CBR%20%2F%3ER%20Send(%20xValues%20)%3B%3CBR%20%2F%3ER%20Send(%20yValues%20)%3B%3C%2FP%3E%3CP%3E%2F%2F%20Execute%20the%20external%20R%20script%3CBR%20%2F%3ER%20Submit%20File(%20%22C%3A%2FUsers%2FNDS%2FData_Ana.R%22%20)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%2F%2F%20Retrieve%20the%20plot%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3BR%20submit(%20%22print(plot1)%22%20)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ER%20submit(%20%22print(plot2)%22%20)%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%2F%2F%20Terminate%20the%20R%20connection%3CBR%20%2F%3E%2F%2FR%20Term()%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-803149%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3EAutomation%20and%20Scripting%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EData%20Exploration%20and%20Visualization%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-803263%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20R-plot%20though%20JSL%2FJMP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-803263%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EI%20believe%20what%20you%20are%20seeing%20is%20plot%202%20immediately%20replaces%20plot%201%20as%20you%20have%20no%20intervening%20Wait()%20or%20code%20to%20capture%20the%20image%20or%20display%20the%20image%20in%20a%20new%20JMP%20window.%20%26nbsp%3BIn%20running%20R's%20plot()%20you%20are%20getting%20an%20R%20window%20not%20a%20JMP%20window%2C%20so%20when%20you%20run%20the%20second%20plot%20command%20it's%20overwriting%20the%20first%20plot.%20%26nbsp%3BFrom%20the%20link%20you%20referenced%2C%20see%20the%20section%20on%20R%20Get%20Graphics().%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETry%20something%20like%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E...%0AR%20Submit(%22print(plot1)%22)%3B%0ANew%20Window(%22Plot%201%22%2C%20Picture%20Box(%20R%20Get%20Graphics(png)%20)%20)%3B%0AR%20Submit(%22print(plot2)%22)%3B%0ANew%20Window(%22Plot%202%22%2C%20Picture%20Box(%20R%20Get%20Graphics(png)%20)%20)%3B%0A...%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EHave%20you%20looked%20at%20the%20examples%20in%20JMP's%20scripting%20index%2C%20that%20is%20located%20under%20the%20Help%20menu.%20%26nbsp%3BThe%20script%20below%20comes%20from%20the%20Scripting%20index%20'R%20Get%20Graphics'%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(%201%20)%3B%0AR%20Init()%3B%0AR%20Submit(%20%22plot(1%3A10)%22%20)%3B%0Aplot%20%3D%20R%20Get%20Graphics(%20png%20)%3B%0ANew%20Window(%20%22Plot%22%2C%20Picture%20Box(%20plot%20)%20)%3B%0AR%20Term()%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-804938%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20R-plot%20though%20JSL%2FJMP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-804938%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%20Paul%20Thank%20you%20for%20your%20answer.%20I%20am%20trying%20to%20repeat%20the%20same%20process%20just%20for%20Python%20instead%20of%20R.%3CBR%20%2F%3E%3CBR%20%2F%3EAnd%20the%20JSL%20keep%20loading%20the%20last%20defined%20plot%20from%20Python%20instead%20of%20plotting%20the%20correct%20figure.%3CBR%20%2F%3E%3CBR%20%2F%3EThe%26nbsp%3Bplt.show(plt.figure(plot1))%20works%20in%20Python%20but%20when%20I%20use%20the%20code%20below%20in%20JSL%3A%3CBR%20%2F%3Eplot1%20%3D%20Picture%20Box(Python%20Get%20Graphics(%22png%22%2C%22plt.show(plt.figure(plot1)%22))%3B%3C%2FP%3E%3CP%3EThen%20JSL%20keep%20takeing%20the%20last%20defined%20plot.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20have%20a%20suggestion%20of%20what%20I%20wo%20wrong%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-805156%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20R-plot%20though%20JSL%2FJMP%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-805156%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EFirst%2C%20from%20the%20documentation%20in%20the%20Scripting%20Index%2C%20%26nbsp%3BSyntax%3A%20image%20%3D%20Python%20Get%20Graphics(%20format%20)%3B%3C%2FP%3E%0A%3CP%3EThere%20is%20no%20second%20parameter.%20%26nbsp%3BThe%20scripting%20index%20description%20states%3A%26nbsp%3B%3CEM%3EReturns%20the%20last%20graphics%20object%20written%20to%20the%20Python%20graph%20display%20window%20in%20a%20graphics%20format%20specified%20by%20the%20format%20parameter.%3C%2FEM%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESecond%2C%26nbsp%3B%3CSPAN%3EYou%20must%20be%20using%20JMP%20prior%20to%20JMP%2018.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20face%3D%22inherit%22%3EPython%20Get%20Graphics(png)%3B%20was%20deprecated%20in%2018%2C%20and%20already%20removed%20from%20JMP%2019%20%3C%2FFONT%3Edevelopment%3CFONT%20face%3D%22inherit%22%3E%26nbsp%3Bbuilds.%20%26nbsp%3BIn%20JMP%2018%20it%20does%20not%20return%20a%20value%2C%20but%20instead%20raises%20an%20Alert%20dialog.%20The%20appropriate%20way%20which%20will%20across%20all%20versions%20of%20JMP%2C%20is%20to%20have%26nbsp%3B%3C%2FFONT%3Ethe%20Python%20code%20save%20the%20image%20to%20a%20file.%20Then%20Open()%20the%20file%20and%20hand%20that%20off%20off%20to%20a%20JSL%20Picture%20Box().%20%26nbsp%3BJMP%2018's%20scripting%20index%20for%20Python%20Get%20Graphics()%20gives%20a%20complete%20example%20of%20the%20workaround.%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInstead%20of%20doing%20a%20plt.show()%3C%2FP%3E%0A%3CP%3Eyou%20should%20do%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%23%20save%20image%20to%20location%20of%20your%20choosing%20%0Aplt.savefig(%20jmp.TEMP%20%2B%20'get_graphics_img.png')%20%23%20JMP%2018%2B%0A%0A%23%20or%20...%20savefig(%20'%2Ftmp%2Fget_graphics_img.png')%20%20before%20JMP%2018%20(macOS).%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3BThen%20from%20JSL%3A%20%26nbsp%3B%3CEM%3Eor%20via%20jmp.run_jsl('''%20...%20'''')%20from%20Python%26nbsp%3B%3C%2FEM%3E%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%2F%2F%20plot%20%3D%20Python%20Get%20Graphics(%20png%20)%3B%20%20%20%20%20%20%20%2F%2F%20DEPRECATED%0ApngJMP%20%3D%20New%20Window(%20%22Plot%22%2C%20Picture%20Box(%20Open(%20%22%24TEMP%2Fget_graphics_img.png%22%2C%20png%20)%20)%20)%3B%0APython%20Submit(%20%22plt.close()%22%20)%3B%0Arc%20%3D%20Delete%20File(%20%22%24TEMP%2Fget_graphics_img.png%22%20)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThere%20is%20another%20issue.%20%26nbsp%3BEspecially%20if%20running%20on%20the%20Mac%2C%20matplotlib%20and%20JMP%20fight%20for%20control%20over%20the%20window%20event%20loop%20if%20matplotlib%20is%20brought%20up%20interactively.%20%26nbsp%3BYou%20can%20get%20an%20exit%20of%20JMP%20by%20closing%20the%20last%20matplotlib%20window%20visible%20on%20screen.%20%26nbsp%3BThis%20depends%20on%20the%20matplotlib%20backend%2C%20but%20it's%20especially%20finicky%20on%20macOS.%3C%2FP%3E%3C%2FLINGO-BODY%3E
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.