Spotfire Tips & Tricks: Create R Graphics in Spotfire

Last updated:
11:53am Nov 05, 2020

Back to main Tips & tricks page

Authors: Douglas Johnson, Kathleen Roan

Overview

The significance of information visualization has been well-established over the last few years in the analytics and data science community. Owing to the wide variety of users and domains that are using data visualization, Spotfire offers multiple ways to create visualizations:

  1. Out of Box menu Visualizations: Spotfire provides an extensive list of visualizations that the user can configure right out of box. These can be linked to show detailed drill downs, combined with data generated dynamically from TERR, and controlled through filtering and marking settings.
  2. Customize using JavaScript in JSViz: Within the TIBCO Spotfire community, one area of discussion that crops up continually is that of novel visualization types. Customers often ask for a domain specific visualization type that Spotfire does not currently support. Others ask for simple customizations of existing visualizations, such as moving labels around, that sound straightforward but are almost impossible to implement without extensive coding, JSViz framework is a good solution to such queries.
  3. Create Open Source R Visualizations in Spotfire: Spotfire supports TERR (TIBCO Enterprise Runtime for R) and Open Source R through Data Functions, but often a user would want to create an R graphics using say ggplot2 library in Spotfire. The bulk of this article will focus on how-to embed R Graphics in a Spotfire Dashboard.
  4. Other methods to extend Spotfire visualizations may include working with Well Known Binary formats, particularly for Geoanalytics. These can be manipulated in data functions through TERR and plotted on map charts directly. TERR also includes options to create interactive plots in RStudio using libraries such as ‘ggvis’.

TIBCO Enterprise Runtime for R is a high-performance, enterprise-quality statistical engine to provide predictive analytic capabilities. TERR is available for integration into other applications through various APIs. Developing in R, and then deploying on TERR, lets you rapidly move from prototyping to production, without recoding and retesting your analyses.

Create a simple R graph in Spotfire

This example uses the TERR engine, the RinR package, and the RGraph() function to return a column of R qqnorm() graphs, and then display them in a Spotfire table visualization. To use the RinR package, you must have installed a compatible version of open-source R, and you must set the path to open-source R so RinR can call R functions. The attached example, qqnormGraphsinDataTableColumn.dxp uses the data set fuel.frame, which is provided in the Sdatasets package in TERR.  

Prerequisites

An installation of open-source R compatible with the version of TERR in Spotfire.

Note: This example is created in Spotfire 11, which includes TERR 5.1.  TERR 5.1 is tested for compatibility with open-source R 3.6.2.  

  1. From the Spotfire menu, click Tools > Register Data Functions.
  2. Provide the name for the data function. We use qqnormGraphsInDataTableColumn.
  3. From the Type drop-down list, select R script - TIBCO Enterprise Runtime for R.

    Hint: You can specify the packages for the data function in the Packages list; for this example, we specify them in the script. (This practices makes data function code more portable.)
  4. Provide a description of the function. The description can be useful for others who might need to edit or review the data function later.
  5. Optionally, clear the Allow caching check box.
  6. In the Script text box, provide the following text.
    # Load the TERR packages.
         library( RinR )
         library(Sdatasets)
    
    # Provide the path to your installation of open-source R. Also add error-catching code to make sure that TERR can find the path. This path is required so that RinR can call R functions.
    
         PathToOpenSourceR <- "C:/Program Files/R/R-3.6.2/bin/R.exe"
             if( ! file.exists( PathToOpenSourceR ) )
                 {
          stop( paste( "The specified path to open-source R (", PathToOpenSourceR, "does not exist in this environment. Please specify a valid path, including the name of the executable, such as 'R.exe', in the data function's TERR script." ) )
                 }
                
         options( RinR_R_FULL_PATH = PathToOpenSourceR )
    
    # Create a named list of Mileage columns. The component names are the Type values, and each component is a vector of the corresponding Mileage values.
         FuelFrameSplit <- split( fuel.frame$Mileage, fuel.frame$Type )
    
    # Specify the graphs' list appearance, size, and color.
          qqnormGraphsList <- 
              lapply( 
              X = FuelFrameSplit,   
              FUN = function(X) 
          RGraph(   
             data = list( X = X ),
             height = 680,
             width  = 680,
             expr =
             {
                par( mar = c(8, 6, 6, 4) + 0.1 )
                qqnorm( X, pch = 16, col = "blue", cex = 3,
                cex.main = 3, cex.axis = 2.5, cex.lab = 3)
                qqline( X, col = "red", lwd = 3)
              } ) )
    		
    # Create the graph data function, specifying the VehicleType and the PlotImage from the graphs' list.
    
    	 qqnormGraphsDF <-
             data.frame(
             VehicleType = names( qqnormGraphsList ),
                PlotImage = seq( along = qqnormGraphsList ),
                stringsAsFactors = FALSE )
    
    # Assign the graph list to the data frame, and label the plot's column name.
    		
         qqnormGraphsDF[["PlotImage"]] <- qqnormGraphsList
  7. Click the Input Parameters tab, and in the dialog box, create the input parameter FuelFrame as type Table, with all allowed data types. Specify as required.
  8. Click the Output Parameters tab, and in the dialog box, add the output parameter named qqnormGraphsDF as type Table.
  9. Save the data function to the library.

Using RGraphics in the web player

In the web player, RinR works with a TIBCO Spotfire Statistics Services instance that uses TERR as its statistical engine. Follow the steps to create RGraphics in Spotfire text areas in the web player:

  1. Download and install a compatible open-source R instance on the server that hosts the TSSS/TERR instance 
  2. Provide the path to that open-source R instance for use in calls to RinR::RGraph()
  3. Install any packages for inside calls to RGraph(). These packages must be installed in the open-source R instance on the Spotfire Statistics Services server. 

    (See Configuring an Open-Source R Engine in the Spotfire Statistics Services documentation for more information.)

    No additional CRAN packages are needed

You can use the example in this article for testing the setup.

 

Advanced Configuration

Configure path to Open Source R

If you have multiple versions of open-source R installed on your computer, you need to point to the correct engine that you want RinR to invoke. You can set this configuration in your script using one of the following exmple snippets:

For TERR 4.4 or later, (in Spotfire 7.14 or later):

PathToOpenSourceR <- "C:/Program Files/R/R-3.5.1/bin/x64/R"

configureREvaluator(REvaluator, FullPath = PathToOpenSourceR)

Error message "<grapics package name> not found"

This error can happen for a number of reasons:

  • The graphics package is not installed in open source R.

    Fix: Install the correct graphics package.
  • Default path to open-source R is not configured correctly, and RinR is calling to a different version.

    Fix:  Correct the path of open-source R in in PathToOpenSourceR.
  • The package was built in a newer open-source R version, so invoking it does not cause an error, but does not succeed either.

    Fix:  Update open-source R.

Writing multi-line code for plots

The print() function is designed for single-line plot code or chained ggplot2 style code. To  write effective multiline code replace print(<plot code here>) with expr = {<multi-line plot code here>}. Alternatively, you can separate multiple print() statements within the body of the RGraph() function using commas.

 

How do I learn more?

This article summarizes how to create a simple R graphic in Spotfire. Watch the page and vote up to get notified about detailed updates. You could also request a featured session on any specific method from above on Dr. Spotfire by:

See also

Attachments

AttachmentSize
Binary Data rgraphics.dxp543.82 KB