RObject methods

Top   Previous   Next

You can get RObject using R facade predefined object in JavaScript.

Each RObject object provides the set of methods:

 

· void newSession () - create new R session, all variables in previous context will be removed.

 

   Example:

        r.newSession();

 

· void saveSession (String name) - associate current context with name and save it on file system. Context can be restored later with openSession function

 

   Example:

        r.saveSession("e;mySession"e;);

 

· void openSession (String name) - remove current R variables and load context from file system by name.

 

   Example:

        r.openSession("e;mySession"e;);

 

· void voidEval (String expression) - evaluate R expression in R environment without result returning.

 

   Example:

        r.voidEval("e;a <- 10"e;); //create variable in R context with integer value

 

· REXP eval (String expression) -  evaluate R expression in R environment. The parameter of this method is string with R script and it returns object as a result of the last R commend.

 

   Example:

        var d = rObject.eval("e;rnorm(2)"e;); //return array of double

 

· void assignObject (String varName, Object value) - assign Javascript object to R environment with specified variable name.

 

   Example:

        rObject.assignObject("e;a"e;, [1,2,3]); //add array variable "e;a"e; to R environment

 

· void help (String expression) - looks for help in R environment and display it in BioUML environment.

 

   Example:

        rObject.help("e;data"e;); //display Data Sets help page from R

 

Complete example

Here is a complete JavaScript example of R session in BioUML.

 

var rObject = R.connect("e;server.biouml.org"e;, 80);   //get remote RObject

print(rObject);                                     //print rObject to check R availability

rObject.newSession();                               //clean R context  

rObject.voidEval("e;x=0;for(i in 1:100) {x = x+i;});  //execute set of commands

var x = rObject.eval("e;x"e;);                          //get x value from R

print(x);                                           //print the result to console

rObject.saveSession("e;testSession"e;);                 //save current variables state

 

To use x variable value next time you can use script like this:

 

var rObject = R.connect("e;server.biouml.org"e;, 80);   //get remote RObject

rObject.openSession("e;testSession"e;);                 //load saved R session

var x = rObject.eval("e;x"e;);                          //get variable from R

print(x);                                           //print x value to console