Subscribe Bookmark RSS Feed

How to create an application using R functions

olivia_manning

Community Trekker

Joined:

Jun 20, 2012

Hi all,

I would be very grateful for any help, as I am new to JMP I am struggling a bit!

I would like to create a tool (would that be an add in?) in JMP (that could be then used by others I need to work with) from functions I have written in R-code.

Is that possible? I have read that it is but I don't even know where to start.

I need this tool to ask the user to put in 3 file paths and let the R-functions use them.

Thank you for your help

21 REPLIES
mewing

Community Trekker

Joined:

Jun 23, 2011

This is fairly easy to do.  The first thing you need to do is create some sort of GUI in JMP that allows the user to input the 3 file paths.  This can be done a couple of ways:


dir = Pick Directory("Where is the file stored?");


This will present the user with a directory browser to select the directory where a file with a consistent name will be stored.

OR


path = Pick File("Which file do you want?");


This will present the user with a file browser to select a specific file.

Next, you need to establish an R connection.


rconn = R Connect();


Now, you have two ways of passing information to R, the first is


rconn << Send(JMPvariable,R Name("name.in.R"));


OR


rconn << Submit("name.in.R <- " || Char(JMPvariable));
rconn << Submit("
      output <- R.function(name.in.R)
      sapply(output,mean)
");


The first way just sends a variable over and initializes it in R.  The second way allows you to submit any R code you want (include the functions you've written).

To get things out of R


JMPvariable = R Get(name.in.R);


A couple of things to note - it takes JMP a little bit of extra time to send code to R - what might take a second in R to run will take several times longer when run through a JSL script.  The nice thing though is that you can use JMP to make beautiful output that R just couldn't do.

If you have any follow-up questions, please ask, I'll subscribe to the thread.

olivia_manning

Community Trekker

Joined:

Jun 20, 2012

Thank you for your help!

However I still have a couple of problems, as I said I am really new to this so there may be simple thing I am missing...

If I use this:

     rconn << Submit("name.in.R <- " || Char(JMPvariable));
     rconn << Submit("
           output <- R.function(name.in.R)
           sapply(output,mean)
     ");

Is "name.in.R" the name of the function I have written in R? If so then does that mean that everytime I want to use this tool in JMP, an R script window will also have to be open with the script for the function?

Or in that space should I be able to write out my whole function (by that I mean the full script for the function)? It doesn't seem to be working for me...

And once this script is written up in JMP, will I be able to add an icon to the toolbar so that all the others have to do is click and enter the files to see the graphs and tables?

Thank you so much for your help, and sorry again if my questions are ridiculous.

mewing

Community Trekker

Joined:

Jun 23, 2011

Your questions aren't ridiculous, everybody was new once (except the people who invented it).

Let's say you have written a function in R like the following:


r6 <- 0
k <- 6


for(i in 1:100000){


x <- rnorm(k+1)


m.i <- 1


for(j in 2:(k+1)){


  if(m.i==1&x>x[j-1]){m.i <- 1}else{m.i <- 0}


}


m.d <- 1


for(j in 2:(k+1)){


  if(m.d==1&x<x[j-1]){m.d <- 1}else{m.d <- 0}


}


r6 <- r6+m.i+m.d


}


perc.ooc <- r6/100000*100


This is just a script that estimates the probability of getting six points in a row all increasing or decreasing from a random normal(0,1) distribution (it relates to some stuffI I'm doing now which is why I'm using it).  Lets say I wanted to let a user change the 'k' value for this rule from 6 to anything of their choice.  There are a couple ways I could approach this, but I'll just show one that uses all the tools above.

First, I have a dialog window that prompts the user for for the value:


nw = New Window("Value for k?",<<Modal,Text Box("k: "),k=Number Edit Box(6));


kval = k << Get();


Now I send that value to R:


rconn << Send(kval,R Name("k");


Now, in my R session (which is invisible to the user) there is a variable called 'k' with whatever value was specified by the user in the dialog box.

Now I want to run my code:


rconn << Submit("
r6 <- 0
for(i in 1:100000){


x <- rnorm(k+1)


m.i <- 1


for(j in 2:(k+1)){


  if(m.i==1&x>x[j-1]){m.i <- 1}else{m.i <- 0}


}


m.d <- 1


for(j in 2:(k+1)){


  if(m.d==1&x<x[j-1]){m.d <- 1}else{m.d <- 0}


}


r6 <- r6+m.i+m.d


}


perc.ooc <- r6/100000*100
");


Note, I removed the like k <- 6 from my code because I've already sent that information to R.  Now, the final step is I want to get the percent out of control so:


percentOOC = R Get(perc.ooc);


I can now display this information in a new window:


nw = New Window("Results",Show Menu(0),Show Toolbars(0),Text Box("Percent out of control is " || Char(percentOOC) || "%"));


Now, all of this is stored in the JSL script file, so there is no need for an R script window to be open.

If you're still having issues, can you post the R script (either here or PM me, I can help you get it put together)?

As far as adding it as a button, that's pretty easy, but I'll post how to do that after we've got your script working.

olivia_manning

Community Trekker

Joined:

Jun 20, 2012

Thanks, I'm starting to get the idea

My R script is made up of 3 functions,

     -read_table(file,n)

     -Summary_Table(file1,file2,file3,n)

     -Test_tables(file1,file2,file3,n,t)

I now see how to get n and t but how to I get the different files (the files being the ones the user has chosen beforehand, I used path = Pick File("Which file do you want?"); as you suggested)?

Could I e-mail you the R-script so that you can have a better idea of what I'm trying to do?

mewing

Community Trekker

Joined:

Jun 23, 2011

Absolutely, if you hover over my name, you should see my email address.

olivia_manning

Community Trekker

Joined:

Jun 20, 2012

It comes up as <private>...

Mine is on there if you send me a blank e-mail then I can send you on the code

mewing

Community Trekker

Joined:

Jun 23, 2011

David_Burnham

Super User

Joined:

Jul 13, 2011

You might find some useful stuff here

http://www.pega-analytics.co.uk/learning-zone/jmp-r-integration/

-Dave
olivia_manning

Community Trekker

Joined:

Jun 20, 2012

Thanks,

That's very nicely done, and very useful!

However, I'm having a problem and I can't seem to find the answer anywhere.

I've got a list of names and I would like to create a new window in which the user can pick one name.

I tried this:

Names = R Get(Tests);

nw = New Window("Test",<<Modal,

     V List Box(

          Check Box(Names);

          H List Box(

               Button Box("Ok", tname = Names << get text; cancel=-1;),

               Button Box("Cancel",cancel=1;)

               )

          )

     );

if(cancel !=-1, Stop(););

I know I must be doing something wrong here but I just can't figure out what...

I was expecting to see a window with a list of check boxes and then "Ok" and "Cancel" underneath but all I've got is "Ok" and "Cancel".

I would be extremely grateful for any help.