I worked a lot with the JMP to R interface when building my JMP add-in, the JMP to R Custom Add-In Builder. So, if you want to learn how to get started with the JMP to R connection using JSL, you’ve come to the right place!
I’m an advocate for learning through examples rather than memorizing APIs, so let’s start with the example of roundtripping data through R and back to JMP. You will need to send data from JMP to R, perform operations in R, and return the results back to JMP. The backbone of your script should thus look like this:
//initializes the R interfaces
//send data to R
R Send(/*name of JMP object you want to send to R, i.e. data table, list, matrix, string, or numeric*/);
R Send File(/*path to the file you want to send here, as a string*/);
//perform calculations in R
R Submit(/*code you want to execute in R here, as a string*/);
R Submit File(/*path to the file that contains the R code you want to execute*/);
//return data from R
varName = R Get(/*name of object you want to return from R*/);
//terminates the R interfaces
Or, perhaps you’d like to do it all in one line, which is equivalent to R Send() for your variable names one by one, R Submit() for your code, and R Get() for R variables one by one:
R Execute(/*list of R input var names*/, /*list of R output var names*/, "\[your R code to submit]\");
One common question I’ve seen is, ‘what is the difference between R Send() and R Submit()?’
R Send() sends a data structure over to R — the five options are data table, list, matrix, string, and numeric.
R Submit(), by contrast, submits R code as a string to R that you want to execute. You can write the code as a string if it does not contain quotation marks or escape it by using “\[ code ]\”.
Once you have this backbone, one question remains: How do you get the R output in a format you can use?
The answer: It depends.
If you want to open up a data table that you retrieved from R, you can use the following command:
R Get(/*name of R variable that contains data table*/) << new data view;
This opens up the data table in JMP.
If you want to append a column in an existing JMP data table, you can use:
varName = R Get(/*name of R variable that contains list with values*/);
dt << New Column(/*name of new column*/, /*column data type, e.g. Character, Numeric*/, Values(varName));
If you want to display the most recently produced R plot, you can use:
R Get Graphics(/*either png or jpg*/);
To view this image in a JMP window, use:
New Window("My Plot", R Get Graphics(/*either png or jpg*/));
One very important note to make: when performing operations such as R Get(), the R variable name must be the name as a JSL expression (i.e. not a string). R Get(“var”), which is incorrect, is not equivalent to R Get(var), which is correct.
Finally, one issue I ran into when using the JMP to R interface was that I relied on user input to determine the parameters for these functions. In this case, I would use Eval(Parse()) to run these JMP to R interface commands. For example:
Eval(Parse(varName || " = R Get(" || userInputValue || ");"));