...
This refactored function getOpals uses three helper functions: init.object.list.testing.environment, init.object.list.global.environment and init.opal.list. Each of these helper function complete a unique aim; the logic of the function becomes easier to read as the body of the selection now holds only one line. Each of these helper functions have a certain level of complexity (i.e., use of iterations and selections), that would be challenging to understand in the getOpals function. Also, the list returned is build using some values.
The getOpals function
This function identifies the R environment used during the execution of the R code. Two possible R environment can be used:
the R environment referred as .GlobalEnvis used by default in the console of R Studio or a R script.
The R environment ds.test_env is used by the testing framework.
For each type of execution, an opal connection object is searched. This part of the implementation can be quite complex. For that reason, the helper functions no 1 and no 2 removes this complexity from the main function. The last step is to build a suitable opal list that can be used to connect to an Opal server.
Code Block | ||
---|---|---|
| ||
getOpals <- function()
|
...
{
|
...
# |
...
initialise function |
...
variables flag <- 0 |
...
|
...
opal.list <- NULL
|
...
return.list <- list("flag"=flag, "opals"=NULL, "opals.list"= NULL) |
...
curr.ds.test_env <- |
...
|
...
get("ds.test_env", envir = .GlobalEnv)
|
...
# Check the computer environment used to execute the code. Then initialise
# accordingly the variable opal.list
if (! is.null(curr.ds.test_env)) |
...
|
...
{ |
...
|
...
opal.list <- init.object.list.testing.environment(ls(curr.ds.test_env))
|
...
} |
...
else |
...
|
...
{ |
...
opal.list <- init.object.list.global.environment(ls(.GlobalEnv)) } |
...
# Build and return the opal list. return.list <- init.opal.list(opal.list) |
...
return(return.list)
} |
helper function no 1
Code Block |
---|
init.object.list.global.environment <- function(objs) { opalist <- vector('list') counter <- 0 for(i in 1:length(objs)) { class.element.name = class(eval(parse(text=objs[i]))) if(class.element.name[1] == 'list') { list2check <- eval(parse(text=objs[i])) if(length(list2check) > 0) { cl2 <- class(list2check[[1]]) for(s in 1:length(cl2)) { if(cl2[s] == 'opal') { counter <- counter + 1 opalist[[counter]] <- objs[i] } } } } } return(opalist) } |
...