R-Scripts in RStat

In this section:

An R-Script can run plots, charts, summaries, model techniques, or even be used to execute scoring functionality using R. As of RStat 1.3.1, R-Scripts can run using RStat, and depending on the technique, the R-Script, if used to create a scoring routine, can be converted to a C routine like a native RStat scoring technique. For both capabilities, the package referenced by the R-Script needs to be loaded to the RStat environment in the R workspace. Without the R package, the script will not run. If the intent of the R-Script is to create a model and output a C file for the scoring routine to be used by the WebFOCUS Reporting Server, then the feature requires a few naming conventions and a few other items to be existent.


Top of page

x
Running an R-Script

In this section:

How to:

To run the R-Script, note the following:



x
Procedure: How to Run an R-Script
  1. Launch RStat from the Command menu in Developer Studio (if you are starting a new process). Otherwise, click the NEW button in the RStat GUI.

  2. On the Data tab, select the RScript option.
  3. Click the Filename list to browse for an R-Script file.

  4. If an R-Script file with an extension of .R or .r is loaded, click the Execute button. The command in R-Script will be loaded in R but no code will be shown in the R GUI.

    The following example shows an R-Script that is executed to create a plot.

    Note: Once the RScript button is active (selected), the user can upload an R-script. When a non-script file is selected (for example, a .csv file or a .txt file), a message appears, as shown in the following image. The error message will also be shown in the RStat status bar and log file. The RScript button is looking for a R code file with an extension of .R.



x
Running an R-Script in Order to Create a C File

To create the Scoring Routine C file from an R-Script, note the following:



x
Procedure: How to Run an R-Script to Create C File Output
  1. Launch RStat from the Command menu in Developer Studio (if starting a new process). Otherwise, click the NEW button on the RStat GUI.

  2. On the Data tab, select the RScript option.
  3. Click the Filename list to browse for an R-Script file.

  4. If an R-Script file with an extension of .R or .r is loaded, click the Execute button. The command in R-Script will be loaded in R but no code will be shown in the R GUI.
  5. If you want to export the model built in the R-Script to a C routine, the following variables must be assigned before exporting (take decision tree as example):
    • crs$dataset: the dataset used to build the model in the script.
    • crs$rpart: model name defined in crs: glm for regression, rpart for decision trees, and hcluster for cluster.
    • pmml.cmd: in the format: pmml.cmd <- "pmml(crs$rpart, dataset=crs$dataset)"
  6. Before clicking export, you must first click the Model tab. Otherwise, the execute action will export a .csv file since the R-Script button resides on the Data tab.
  7. Provide the name for the C file to be created.



x
Reference: Usage Notes for R-Script to Create Scoring Routines

If you choose not to load the data set in RStat but want to export a C routine, you can load an .RData file or use a saved pmml/XML file to export the C routine in the R console with RStat opened. RStat will check crs$dataset before exporting any file. Thus, non-data exporting only can be done in the R console with an RStat session opened.

The .RData file saves the model summary as an R workspace. The example below shows related codes for the decision tree.

decTree<-rpart(PRICE_1991~.,dataset)  
save( decTree, file = 'wine_training_rpart.rdata')

The two code lines above should be included in the user script.

The following code will be the script uploaded by the RScript button in RStat 1.4, which first loads the .RData file to pass the model summary to crs$rpart, and then further defines the 'pmml.cmd' command.

load('wine_training_rpart.rdata')
crs$rpart<-decTree
pmml.cmd <- "pmml(crs$rpart)"

In the R console, executing the following code will export the C routine:

con <- file(dec tree.c", open="w")
cat(pmmltoc(toString(eval(parse(text=pmml.cmd))),
            name="dec tree",  includePMML=TRUE,  includeMetaData="",
            exportClass=FALSE),   file=con)
close(con)

If you generate a pmml\XML file and want to export a C routine, you need to execute the following code lines with RStat open:

cat(pmmltoc(toString("dec tree.xml")
, "dec tree", TRUE, "", FALSE), file = "dec tree.c")

WebFOCUS