How to create a graph using TERR, by calling open-source R functions via the RinR package

How to create a graph using TERR, by calling open-source R functions via the RinR package

book

Article ID: KB0070200

calendar_today

Updated On:

Products Versions
Spotfire Enterprise Runtime for R All

Description

TIBCO Enterprise Runtime for R (TERR) does not create graphics. However, you can call out to an open-source R installation to generate graphics using the RinR package. You can save the graph created in this way to a file, or you can pass the graph object back into Spotfire for display as a static graph.

Issue/Introduction

How to create a graph using TERR, by calling open-source R functions via the RinR package.

Environment

All

Resolution

The following TERR code will create a graph using open-source R:
library( RinR )
library( Sdatasets )

# If running from a Spotfire data function, you will need to
# set the path to your local open-source R installation. 
# For example: 
#   pushPATH( "C:\\Program Files\\R\\R-3.3.2\\bin" )

graph <- RGraph(
      expr =
      {
        par( mfrow = c(3, 2) )
        plot(
            lm( fuel.frame$Mileage ~ fuel.frame$Weight ),
            which = 1:6,
            ask = F )
      },
      data = list( fuel.frame = fuel.frame ),
      display = FALSE
)


You can add code to save the graph to file (i.e. png, jpg, etc...), as in this example:
library( RinR )
library( Sdatasets )

if( ! dir.exists( "C:\\MyGraphicsTestDir" ) )
{
  dir.create( "C:\\MyGraphicsTestDir" )
}

# If running from a Spotfire data function, you will need to
# set the path to your local open-source R installation, 
# for example: 
#   pushPATH( "C:\\Program Files\\R\\R-3.3.2\\bin" )

graph <- RGraph(
      expr =
      { 
        png( file = "C:\\MyGraphicsTestDir\\myplot.png", bg = "transparent")
        par( mfrow = c(3, 2) )
        plot(
            lm( fuel.frame$Mileage ~ fuel.frame$Weight ),
            which = 1:6,
            ask = F
        )
        dev.off()
      },
      data = list( fuel.frame = fuel.frame ),
      display = FALSE
)

Or you can return the graph to Spotfire as a binary object, for display in Spotfire when calling the following code from a Spotfire data function:
library( RinR )
library( Sdatasets )

# Set the path to your local open-source R installation:

pushPATH( "C:\\Program Files\\R\\R-3.3.2\\bin" )

graph <- RGraph(
      expr =
      {
        par( mfrow = c(3, 2) )
        plot(
            lm( fuel.frame$Mileage ~ fuel.frame$Weight ),
            which = 1:6,
            ask = F )
      },
      data = list( fuel.frame = fuel.frame ),
      display = FALSE
)
graph <- as.raw( graph )

To see a full step-by-step example, please refer to the Spotfire Knowledge Base article #000020714 (https://support.tibco.com/s/article/Tibco-KnowledgeArticle-Article-43727).
 

Additional Information

KB 000020714 How to display a binary object as a static graph in Spotfire
You can read more about the functions used in the code above by typing a question mark followed by the function name at the TERR command prompt.  For example:

     ?pushPATH
     ?REvaluate
     ?
RGraph

Note, the RinR package must be loaded (using the command 'library(RinR)') before you can use the question mark operator ("?") to access the help files for the functions in the package.