Spotfire Tips & Tricks: Create R Graphics in Spotfire
Last updated:
11:31am Sep 19, 2018

Back to main Tips & tricks page

Author: DJ Rajdev
Contributors: Douglas Johnson

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

The package ‘RinR’ allows the user to run Open Source R code in TIBCO Enterprise Runtime for R or vice-versa. We’ll be making use of ‘RGraph’ function from ‘RinR’ to create a graphic in OSR and pass it to TERR to display in Spotfire.

Prerequisites

  1. Install ‘RinR’ in TERR (tutorial)
  2. Install the graphics package (say ggplot2, ggally etc.) in Open Source R
  3. Optional. Download the sample heightweight dataset and read in using ‘load()’. This will be stored in Table1 for the scripts below.

Steps

  1. Try it in RStudio with Open Source R

    The following sample script creates a matrix of plots

    library(GGally)
    ggpairs(data = Table1, columns=c(1,3:6))
    
  2. Set up Data Function

    Sample script:

    # @Author: DJ Rajdev
    # @Purpose: Sample RGraph creation
    
    library(RinR)
    
    outputPlot1 <- RGraph(
        print( ggpairs( data = Table1,
                        columns=c( 1,3:6 )
                      )
        ),
        packages='GGally',
        data='Table1'
    )

    Ensure data function is executing in TERR

  3. Define Input and Output Parameter

    Input:

    Output:

  4. Run data Function

    This registers it to the list of data Functions. You can also save this to library

  5. Connect Input and Outputs

    Input - the data table:

    Output - document property RGraphPackage:

  6. Setup Display

    In a text area include the Document Property RGraphPackage as a ‘Label’ type property control

    This displays the resulting graph in a text area.

How the Script works

‘RGraph’ gets the graph from OSR in binary format that can then be stored as a binary value in a Document Property and displayed in a text area. ‘data’ is used to specify the data table being referenced within the graphing commands and ‘packages’ tells OSR which packages to invoke- this argument can take a vector.

Additional Examples

RinR can be used to rapidly prototype R models and display ggplot results as below or rapidly create color palettes for Spotfire.

Using RGraphics in webplayer

In the webplayer RinR would work via a TSSS instance that uses TERR as its statistical engine. Follow the steps to create RGraphics in Spotfire text areas in webplayer:

  • Download and install a compatible open-source R instance on the server that hosts the TSSS/TERR instance 
  • Provide the path to that open-source R instance for use in calls to RinR::RGraph()
  • Any CRAN packages that are needed inside calls to RGraph() will need to be installed in the open-source R instance on the TSSS server
  • No additional CRAN packages are needed

The example in this public KB article can be used for testing the setup.
 

Advanced Configuration

Configure path to Open Source R

If you have multiple versions of open source R, it becomes inportant to tell RinR which one to invoke. This can be configured in the script through following exmple snippets:

For TERR 4.4 +, Spotfire 7.14+
PathToOpenSourceR <- "C:/Program Files/R/R-3.5.1/bin/x64/R"
configureREvaluator(REvaluator, FullPath = PathToOpenSourceR)

For older TERR, Spotfire versions 
PathToOpenSourceR <- "C:/Program Files/R/R-3.5.1/bin/x64/R"
options( RinR_R_FULL_PATH = PathToOpenSourceR )

Error message "<grapics package name> not found"

This can happen for multiple reasons:

  • The graphics package is not installed in open source R.
  • Default OSR is not configured and RinR is calling to a different version.
  • The package was built in a newer OSR version, so invoking it does not cause an error, but does not succeed either. Update OSR.

Writing multi-line code for plots

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

How do I learn more?

This summarizes how to create R graphics 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