Closer Look at R Statistics Scripts Called by WebFOCUS

Let's take a closer look at the R scripts WebFOCUS generating within my sample text-mining application.



In this simple application, WebFOCUS generates one of three graph options: box plot, histogram, or a plot, depending on the user selection.

CRAN


If you do not have R already installed, you can get a copy and full implementation instructions from the Comprehensive R Archive Network--a kind of App Store for all things R. The hyperlink for the CRAN is: https://www.r-project.org/.

For WebFOCUS to be able to communicate easily with R, we want to install R on either the same computer as the BI product or within its network of accessible drives.



When you code in the R programming language, you are commonly using assignment statements and functions.

For example, I want to assign the values in my WebFOCUS-generated text file into a named R data structure. Assignments are done with a symbol that puts together a left-caret with a dash.

To pull out those values, I must first read the text file using a read function. Here is the basic command:



I want a data structure called "keywords" to hold my WebFOCUS file. The read command has a variety of options; I am using the "table" option to read a simple tabular structure. My table has a row identifying the column headers, so I inform read of that with a parameter called header. My table values are separated by commas, so I tell read about that using the sep parameter.

After that command, R has loaded my WebFOCUS data table into its memory.

Before I actually create an output graph, I need to tell R where to store it. Had I been working with R interactively, the graph would just show on the screen. Here, I need to save the graph into a bitmap file and later in the process tell WebFOCUS to display it on the screen.

I do that with the R png command (PNG stands for Portable Network Graphics). Here is a sample:



Along with the output file's full path, I also include some optional parameters to tell R how tall and wide to make the graph.

Two R commands down and just one more to make the graph!

Boxplot


If the user requested a box plot, I would use the R function called, naturally enough, boxplot. My sample statement looks like this:



The first parameter for the boxplot function tells it to use a column called "Count" in my keywords data structure. Notice that R uses the US dollar sign to separate the structure and column name (in this example, keywords$Count). I am also passing in some optional variables to make the box plot lay down horizontally, give R to option to vary its width, and set the X axis label to say "Search Counts."

The boxplot function gives me a bitmap graph such as this:



Histogram


The other R graph functions look very similar. For example, my histogram command is simply:



The difference here is a parameter called main where I am blanking out the main title. My output histogram might look like this:



Plot


Next is the simple graph of points plotted, whose R function looks like this:



This gives me a graph such as the following:




Quick Clean-Up!


Before the R script ends and returns control to WebFOCUS, I am going to tell R to close the output graph file (otherwise, R controls the picture and I cannot open it). To do that, I call a function called "dev" (short for device) and turn off the graphics device connected to the output. It is simply this:



One Last Thing


The three types of graphs used above are all standard R features. Some, however, require that an add-on "package" to be installed and referenced. You can read about this on the CRAN website.

For example, I might want WebFOCUS to create a file of keyword counts and then produce a word cloud. Within R, I would need to install the package containing that feature. Once installed, WebFOCUS needs to reference that package with the "library" function.

Here is an example of WebFOCUS generating a word cloud R script:



If you do have R scripts that use a package, you must have WebFOCUS start R using the R_LIBS parameter to identify where you installed the package. See an earlier article on how that works.

Do You Have WebFOCUS and R?

 
I hope you enjoyed this multi-part article on integrating WebFOCUS and the R statistical programming language. Do you have both products and, if so, do you use them together? Please leave a comment and let us know your experience.
Next Post Previous Post