How to use the "data" and "expr" arguments in the REvaluate() function from TERR's "RinR" package to send data and expressions out to an open source R session.

How to use the "data" and "expr" arguments in the REvaluate() function from TERR's "RinR" package to send data and expressions out to an open source R session.

book

Article ID: KB0081590

calendar_today

Updated On:

Products Versions
Spotfire Enterprise Runtime for R -
Spotfire Statistics Services -

Description

TIBCO Enterprise Runtime for R (TERR) comes with an optional built-in "RinR" package, which makes it possible to send data and expressions (R commands) out to an open-source R instance that has been installed on the same machine. It helps to become acquainted with the "RinR" package's "REValuate()" function and its syntax using a simple round-trip example before providing a more complex R script in the function's "expr" argument. This article provides such an example. Note that this article assumes that you are already familiar with the R programming language.
 

Issue/Introduction

How to use the "data" and "expr" arguments in the REvaluate() function from TERR's "RinR" package to send data and expressions out to an open source R session.

Environment

All supported environments.

Resolution

TIBCO Enterprise Runtime for R (TERR) comes with an optional built-in "RinR" package, which makes it possible to send data and expressions (R commands) out to an open-source R instance that has been installed on the same machine. 
 
It helps to become acquainted with the "RinR" package's "REValuate()" function and its syntax using a simple round-trip example before providing a more complex R script in the function's "expr" argument.  
 
For the same reason, this introductory example uses a TERR Console session without the additional steps involved in registering a Spotfire data function that uses TERR.
 
 
 
Do the following:
 
 
1) Download a compatible version of open-source R from the open-source R community's CRAN repository.
 
 
2) Install open-source R on the machine your Spotfire 7.0 or later desktop client is installed on.
 
 
3) Using Windows Explorer, copy the full path to the R executable (such as "C:\Program Files\R\R-3.2.3\bin\R.exe"), and paste it into a Notepad text editor session for editing. You will need to provide this path in a TERR-readable format in the test script shown below.
 
 
4) Use Notepad's "Edit > Replace" option to replace all backslashes ("\") in the path with forward slashes ("/") so that it will look something like the following after editing:
 
     "C:/Program Files/R/R-3.2.3/bin/R.exe"
 
 
5) Open a new Notepad text editor session and paste the following round-trip test script into this second Notepad session:
 
    ######################################  
    library( "Sdatasets" ) 
    library( "RinR" )
 
    ActualPathToOpenSourceR <- "C:/Program Files/R/R-3.1.2/bin/R"
 
    options( RinR_R_FULL_PATH = ActualPathToOpenSourceR )
 
    Input_DataTable <- fuel.frame
 
    dim( Input_DataTable )
 
    TestData <- REvaluate(   
       data = list( result = Input_DataTable ),
       expr = 
       {
         result[ 1:10, ]
       } )
 
    TestData
 
    dim( TestData )
 
    ######################################  
 
 
6) Copy your edited actual path to the open-source R instance (with forward slashes ("/") as path delimiters) from the first Notepad session. Then paste it into the second line in the second Notepad session's TERR script. In this example, the line in the script that originally read as follows,
 
        ActualPathToOpenSourceR <- "C:/Program Files/R/R-3.1.2/bin/R"
 
would read as follows after pasting the edited actual path to open-source R into it:
 
        ActualPathToOpenSourceR <- "C:/Program Files/R/R-3.2.3/bin/R.exe"
 
 
In this example, the edited script would then look like the following:
 
    ######################################  
    library( "Sdatasets" ) 
    library( "RinR" )
 
    ActualPathToOpenSourceR <- "C:/Program Files/R/R-3.2.3/bin/R.exe"
 
    options( RinR_R_FULL_PATH = ActualPathToOpenSourceR )
 
    Input_DataTable <- fuel.frame
 
    dim( Input_DataTable )
 
    TestData <- REvaluate(   
       data = list( result = Input_DataTable ),
       expr = 
       {
         result[ 1:10, ]
       } )
 
    TestData
 
    dim( TestData )
 
    ######################################  
 
 
Note all of the following important syntax items.
 
- This example TERR script explicitly passes the "data" argument and the "expr" argument into 'REvaluate()' by name, and it also provides the "data" argument as a named list, using this syntax:
 
     data = list( result = Input_DataTable ),
 
- The name provided in the named list is the object name that must be used when referring to that data object in the expression passed to open-source R via the 'REvaluate()' function's "expr" argument, as shown here:
 
     expr = 
       {
         result[ 1:10, ]
       } )
 
- The expression that is sent to the open-source R instance via the "expr" argument must be enclosed in a pair of curly braces {}, as shown above.
 
 
7) Use your Spotfire 7.0 or later desktop client's "Tools > TERR Tools > Launch TERR Console" main-menu option to open a new TERR Console session. Then paste your edited version of this round-trip test of REvaluate() function syntax into the TERR Console session and press the Enter key.  
 
 
The following as-run script shows how this example works in a TERR Console session that is running TERR 4.0.2: 
 
==========================================================  
TIBCO Software Inc. Confidential Information
Copyright (C) 2011-2015 TIBCO Software Inc. ALL RIGHTS RESERVED
TIBCO Enterprise Runtime for R version 4.0.2 for Microsoft Windows 64-bit
 
Type 'help()' for help.
Type 'q()' to quit.
>
>
>
>
>     ######################################
>     library( "Sdatasets" )
>     library( "RinR" )
>
>
>     ActualPathToOpenSourceR <- "C:/Program Files/R/R-3.2.3/bin/R.exe"
>
>
>     options( RinR_R_FULL_PATH = ActualPathToOpenSourceR )
>
>
>     Input_DataTable <- fuel.frame
>
>
>     dim( Input_DataTable )
[1] 60  5
>
>
>     TestData <- REvaluate(
+        data = list( result = Input_DataTable ),
+        expr =
+        {
+          result[ 1:10, ]
+        } )
>
>
>     TestData
                 Weight Disp. Mileage     Fuel  Type
Eagle Summit 4     2560    97      33 3.030303 Small
Ford Escort 4      2345   114      33 3.030303 Small
Ford Festiva 4     1845    81      37 2.702703 Small
Honda Civic 4      2260    91      32 3.125000 Small
Mazda Protege 4    2440   113      32 3.125000 Small
Mercury Tracer 4   2285    97      26 3.846154 Small
Nissan Sentra 4    2275    97      33 3.030303 Small
Pontiac LeMans 4   2350    98      28 3.571429 Small
Subaru Loyale 4    2295   109      25 4.000000 Small
Subaru Justy 3     1900    73      34 2.941176 Small
>
>
>     dim( TestData )
[1] 10  5
>
>
>     ######################################
==========================================================  
 
 
Note that the "data" and "expr" arguments in the "RinR" package's "RGraph()" function work the same as they do in the "REvaluate()" function.
 
 

Additional Information

Help file that is opened in a web browser when the following command is issued at the TERR Console command prompt:
 
      help( "REvaluate", try.all.packages = TRUE )
 
 
Spotfire desktop help topics for "TERR Tools":
     - Go to the online help for TIBCO Spotfire (desktop client / Analyst / Professional) 7.0:
           https://docs.tibco.com/pub/spotfire/7.0.0/doc/html/index.htm
     - In the left-hand pane of the help page, left-click on the [+] signs to expand the following tree of topics:
       -- Tools
           --- TERR Tools
     - Double-click on the following topic, to display it in the right-hand pane of the help page:
               ---- TERR Tools - Details on Engine