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("mySession");
| • | void openSession(String name) - remove current R variables and load context from file system by name. |
Example:
r.openSession("mySession");
| • | void voidEval(String expression) - evaluate R expression in R environment without result returning. |
Example:
r.voidEval("a <- 10"); //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("rnorm(2)"); //return array of double
| • | void assignObject(String varName, Object value) - assign Javascript object to R environment with specified variable name. |
Example:
rObject.assignObject("a", [1,2,3]); //add array variable "a" to R environment
| • | void help(String expression) - looks for help in R environment and display it in BioUML environment. |
Example:
rObject.help("data"); //display Data Sets help page from R
Complete example
Here is a complete JavaScript example of R session in BioUML.
var rObject = R.connect("server.biouml.org", 80); //get remote RObject
print(rObject); //print rObject to check R availability
rObject.newSession(); //clean R context
rObject.voidEval("x=0;for(i in 1:100) {x = x+i;}); //execute set of commands
var x = rObject.eval("x"); //get x value from R
print(x); //print the result to console
rObject.saveSession("testSession"); //save current variables state
To use x variable value next time you can use script like this:
var rObject = R.connect("server.biouml.org", 80); //get remote RObject
rObject.openSession("testSession"); //load saved R session
var x = rObject.eval("x"); //get variable from R
print(x); //print x value to console