Jump to content
  • Spotfire Tips & Tricks: Create R Graphics in Spotfire


    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.

    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:

    • Out of the box menu visualizations: Spotfire provides an extensive list of visualizations that the user can configure right out of the box. These can be linked to show detailed drill downs, combined with data generated dynamically from TERR, and controlled through filtering and marking settings.
    • Customize using JavaScript in JSViz: Within the 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.
    • Create Open-Source R visualizations in Spotfire: Spotfire supports TERR (Spotfire Enterprise Runtime for R) and Open-Source R through data functions, but often a user would want to create R graphics using, say, the ggplot2 library in Spotfire. The bulk of this article focuses on how to embed R Graphics in a Spotfire Dashboard.
    • Other methods to extend Spotfire visualizations can 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.

    Spotfire Enterprise Runtime for R is a high-performance, enterprise-quality statistical engine that provides predictive analytic capabilities. TERR is available for integration into other applications through various APIs. By developing in R, and then deploying on TERR, you can 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, first install a compatible version of open-source R and 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. If you are using an installation of Spotfire Enterprise Runtime for R - Server Edition (TERR service) installed on a node for your Spotfire Server, rather than Spotfire Statistics Services server, then you must also install a compatible version of the R engine on the node.

    Note: Spotfire Statistics Services is discontinued, with required updates to the LTS version 12.x only. 

    Note: This example is created in Spotfire 12, which includes TERR 6.1.  TERR 6.1 is tested for compatibility with open-source R 4.0.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 - Spotfire 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 practice makes data function code more portable.)

    4. Describe 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. Select 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. Select 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 Spotfire Statistics Services instance that uses TERR as its statistical engine, or a TERR service instance installed on a Spotfire Server node. 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 or TERR service 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 or the node manager running the TERR service. 

      (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 example snippets:

    For TERR 6.1 or later, (in Spotfire 12.x or later):

    PathToOpenSourceR <- "C:/Program Files/R/R-4.0.2/bin/x64/R"
    configureREvaluator(REvaluator, FullPath = PathToOpenSourceR)

    Error message "<graphics package name> not found"

    This error can happen for several reasons:

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

      Fix: Install the correct graphics package.

    • The 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 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

    rgraphics.dxp


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...