cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
bluenose
Level III

JSL: How to loop through columns, generate a Graph Builder report, and save each as a JPG file

Hello,

 

I have a dataset contianing 41 variables. I need to plot each of the first 40 against the last one, for example, to make 40 box plots, or jitter plots.

 

To create a nice fully featured plot via the create graph menu it's easy and even fun. I know how to save the script of what I did, but what I don't know, is how to write a loop that will take each iteration another variable and replace it in my original graph code. In other words, if I have 20 lines of code, I want a loop that will run the same code over and over again, and will change only the place where I define the dependent variable. In addition, I need a nice way of showing the results (40 windows are not practical), so I thought maybe to save each one as a JPG pre moving to next iteration. Is this possible with JMP ?

 

Thank you  !

1 ACCEPTED SOLUTION

Accepted Solutions

Re: JSL Loop

The following example demonstrates how to obtain a list of column names as character strings.  The For loop generates an invisible Graph Builder beginning with the second column in the list because the first is used as the X.  (The original post would need to adjust these to start with the first column in the list and stop before the last column in the list.)  Each Graph Builder report is saved as a JPG file where the column name is included in the filename.  Finally, the data table and invisible reports are closed.

/* Open a sample data table */
dt = Open( "$SAMPLE_DATA\Fitness.jmp" );

/* Obtain a list of numeric/continuous column names as strings */
colList = dt << Get Column Names( Continuous, String );

/* Loop through the list to generate the desired Graph Builder 
   and save the report as a JPEG file */
For( i = 2, i <= Nitems( colList ), i++,
	gb = dt << Graph Builder(
		Size( 534, 454 ),
		Show Control Panel( 0 ),
		Variables(
			X( :Age ),
			Y( Column( dt, colList[i] ) ),
			Color( :Age )
		),
		Elements(
			Points(
				X,
				Y,
				Legend( 3 ),
				Summary Statistic( "Mean" ),
				Error Bars( "Confidence Interval" )
			)
		),
		SendToReport(
			Dispatch(
				{},
				"Graph Builder",
				FrameBox,
				{Marker Size( 5 )}
			)
		), 
		Invisible
	);
	gb << Save Picture( "E:\Trash\Graph Builder_" || colList[i] ||".jpg", JPEG );
);

/* Close table and all invisible reports */
Close( dt, No Save );

Hope that helps!

Wendy

View solution in original post

3 REPLIES 3

JSL Loop

Howdy!  What you want to do is fairly easy.

 

First, in your script you will want to define the current data table:

cd = Current Data Table();

Now, get the list of column names:

column_names = cd << Get Column Names;

 Now, there are multiple ways to do the next part, but I find this to be the easiest.  You'll want to start a text string that we will eventually parse and evaluate.  The first part is to create a new window.

 

nw = "New Window(\"Graphs\",
     randomname = V List Box(";

 

Now we can write the loop which will insert your custom code with one minor alteration.  I'll assume you know how to use a JMP 'for' loop - I'm just going to iterate over all but the last column in the table.

 

 

for(i=1,i<=length(column_names)-1,i++,
nw = nw || "PUT YOUR CUSTOM CODE HERE*" || ",";
);

 

 

*In your custom code, where you define the dependent variable, remove it and replace it with

:Name(\"" || column_name || "\")

What this will do is paste in the text name of each dependent variable you have within the custom script you've written.

Finally, we need to finish the new window we started earlier:  The first line finishes writing the code to make the new window, the second line first parses the text to make it code and then evaluates the code contained in nw.

 

nw = nw || "));";
eval(parse(nw));

That should do it - let me know if you have any questions or need specific help!

bluenose
Level III

JSL Loop

Hi,

 

thanks for your attempt to help. Unfortunatelly, I am still not familier enough with JSL (when I'll have a break from work I'll get deep into it), and I got troubles with the brackets and all that, so your suggestion didn't work, I probably had one sign in the wrong place.

 

My original code is:

 

 

Graph Builder(
Size( 880, 750 ),
Show Control Panel( 0 ),
Variables(
  X( :Cutoff, Size( 0, 21 ) ),
  Y( :S1, Size( 0, 22 ) ),
  Color( :Cutoff )
),
Elements(
  Points( X, Y, Color, Legend( 1 ), Jitter( 1 ) ),
  Line(
   X,
   Y,
   Color,
   Legend( 2 ),
   Row order( 0 ),
   Error Bars( "Confidence Interval" ),
   Summary Statistic( "Mean" )
  )
),
SendToReport( Dispatch( {}, "Graph Builder", FrameBox, {Marker Size( 5 )} ) )
);

 

Can you please help me to wrap it with the loop ?

 

Cheers!

 

 

Re: JSL Loop

The following example demonstrates how to obtain a list of column names as character strings.  The For loop generates an invisible Graph Builder beginning with the second column in the list because the first is used as the X.  (The original post would need to adjust these to start with the first column in the list and stop before the last column in the list.)  Each Graph Builder report is saved as a JPG file where the column name is included in the filename.  Finally, the data table and invisible reports are closed.

/* Open a sample data table */
dt = Open( "$SAMPLE_DATA\Fitness.jmp" );

/* Obtain a list of numeric/continuous column names as strings */
colList = dt << Get Column Names( Continuous, String );

/* Loop through the list to generate the desired Graph Builder 
   and save the report as a JPEG file */
For( i = 2, i <= Nitems( colList ), i++,
	gb = dt << Graph Builder(
		Size( 534, 454 ),
		Show Control Panel( 0 ),
		Variables(
			X( :Age ),
			Y( Column( dt, colList[i] ) ),
			Color( :Age )
		),
		Elements(
			Points(
				X,
				Y,
				Legend( 3 ),
				Summary Statistic( "Mean" ),
				Error Bars( "Confidence Interval" )
			)
		),
		SendToReport(
			Dispatch(
				{},
				"Graph Builder",
				FrameBox,
				{Marker Size( 5 )}
			)
		), 
		Invisible
	);
	gb << Save Picture( "E:\Trash\Graph Builder_" || colList[i] ||".jpg", JPEG );
);

/* Close table and all invisible reports */
Close( dt, No Save );

Hope that helps!

Wendy