Using WebFOCUS to Call R
Using the WebFOCUS Dialogue Manager command to create symbolic variables, I set the values of three different folder locations.
In this second of several articles on integrating WebFOCUS and R, I will show how the enterprise BI product from Information Builders can easily call the open-source R statistical programming language. If you have not already, go back and read Part I for an overview.
One simple way for WebFOCUS to call the R product is to use the Dialogue Manager scripting language and the SYSTEM function.
For this example, I created a WebFOCUS procedure named "call_r_dynamic" intended to be executed from within other WebFOCUS procedures. The calling procedure uses a symbolic variable named &SCRIPT to pass in the name of the R script to be run.
Using the WebFOCUS Dialogue Manager command to create symbolic variables, I set the values of three different folder locations. Now typically, symbolics are intended to provide dynamically-changing values for parameters, but I am really just leveraging them here for readability purposes.
They are:
- &R_BINARY variable tells WebFOCUS where the R executable is stored
- &R_LIBS variable tells WebFOCUS where R packages are stored
- &R_HOME variable tells WebFOCUS where the R scripts are stored
-SET &R_BINARY = '"C:\Program Files\R\R-2.15.1\bin\i386\R"';
-SET &R_LIBS = 'C:\Users\Doug\Documents\R\win-library\2.15';
-SET &R_HOME = 'C:\ibi\apps\r_scripts';
Notice that if any of your folder names have spaces, you need to enclose them within double quotes. Otherwise, the procedure gets confused when it hits the blank and thinks the folder name ends there. Also notice that I store the R scripts in an application folder on the WebFOCUS Reporting Server (I will talk more about this in a later post).
The name of the R script really is a parameter from the calling program--for example, something like "r_dynamic_graph"--so I dynamically set its full path as follows:
-SET &R_SCRIPT = &R_HOME || '\' || &SCRIPT;
With those four symbolic variables, I will put everything together into a single instruction call to R:
-SET &R_CALL = '&R_BINARY.EVAL HOME=&R_HOME.EVAL R_LIBS=&R_LIBS.EVAL --vanilla < &R_SCRIPT.EVAL ';
At run-time, WebFOCUS will substitute all of my values into a string such as this:
"C:\Program Files\R\R-2.15.1\bin\i386\R" HOME=C:\ibi\apps\r_scripts R_LIBS=C:\Users\Doug\Documents\R\win-library\2.15 --vanilla < C:\ibi\apps\r_scripts\r_dynamic_graph.R
This command string will call R, telling it to use my R libraries. Notice that I included a start-up option called "vanilla" which basically instructs R to do minimal work. Also important is the left caret, telling R to run the script whose name I provided.
I still need to execute this command string, which is where the WebFOCUS SYSTEM function comes into play. This will pass the command string to the underlying operating system and send back a return code showing success or failure.
The WebFOCUS SYSTEM function takes three parameters:
SYSTEM(commandstring_length, commandstring, returncode_format);
Here is my actual WebFOCUS Dialogue Manager code using the SYSTEM function to call R:
-SET &&RETCODE = SYSTEM(&R_CALL.LENGTH,&R_CALL,'D4');
I put the SYSTEM's return code into a global symbolic variable called &&RETCODE so that the calling procedure can verify everything worked.
In Part III of this article, I will show how to use WebFOCUS to generate and run dynamic R scripts.
Notice that if any of your folder names have spaces, you need to enclose them within double quotes. Otherwise, the procedure gets confused when it hits the blank and thinks the folder name ends there. Also notice that I store the R scripts in an application folder on the WebFOCUS Reporting Server (I will talk more about this in a later post).
The name of the R script really is a parameter from the calling program--for example, something like "r_dynamic_graph"--so I dynamically set its full path as follows:
-SET &R_SCRIPT = &R_HOME || '\' || &SCRIPT;
With those four symbolic variables, I will put everything together into a single instruction call to R:
-SET &R_CALL = '&R_BINARY.EVAL HOME=&R_HOME.EVAL R_LIBS=&R_LIBS.EVAL --vanilla < &R_SCRIPT.EVAL ';
At run-time, WebFOCUS will substitute all of my values into a string such as this:
"C:\Program Files\R\R-2.15.1\bin\i386\R" HOME=C:\ibi\apps\r_scripts R_LIBS=C:\Users\Doug\Documents\R\win-library\2.15 --vanilla < C:\ibi\apps\r_scripts\r_dynamic_graph.R
This command string will call R, telling it to use my R libraries. Notice that I included a start-up option called "vanilla" which basically instructs R to do minimal work. Also important is the left caret, telling R to run the script whose name I provided.
I still need to execute this command string, which is where the WebFOCUS SYSTEM function comes into play. This will pass the command string to the underlying operating system and send back a return code showing success or failure.
The WebFOCUS SYSTEM function takes three parameters:
SYSTEM(commandstring_length, commandstring, returncode_format);
Here is my actual WebFOCUS Dialogue Manager code using the SYSTEM function to call R:
-SET &&RETCODE = SYSTEM(&R_CALL.LENGTH,&R_CALL,'D4');
I put the SYSTEM's return code into a global symbolic variable called &&RETCODE so that the calling procedure can verify everything worked.
In Part III of this article, I will show how to use WebFOCUS to generate and run dynamic R scripts.