How to Create an Expression Function


Use the Expression Function dialog to register a function in the Spotfire expression language that is based on Spotfire® Enterprise Runtime for R (a/k/a TERR™). This instruction includes an example of how to write a simple script to create a temperature conversion function.

Tip: If you are developing scripts using Spotfire Enterprise Runtime for R, you can use RStudio, a full-featured, open-source integrated development environment for working with R code. It is provided independently of Cloud Software Group, Inc. You can configure RStudio to use the Spotfire Enterprise Runtime for R engine and to display its language reference. Also, you can access the Spotfire Enterprise Runtime for R language reference at the documentation site.

To register an Expression Function in Spotfire:

  1. On the menu bar, click Data > Data functions properties.

  2. Response: The Data Function Properties dialog is displayed.

  3. Click the Expression Functions tab.

  4. Click New.
    Response: The Expression Function dialog is displayed.

  5. Type a Name for your Expression Function.
    Comment: The name of the Expression Function will be shown when browsing the Functions list in the Custom Expression and Add Calculated Column dialogs.

  6. Select the Function type: A Column function returns the same number of rows as the input, whereas an Aggregation function aggregates the input values into a single output value.

  7. Optionally, type a Description for your Expression Function.
    Comment: The description of the Expression Function is displayed when browsing the Functions list in the Custom Expression and Insert Calculated Column dialogs.

  8. In the Script field, enter the script. For example, paste the following script to create a temperature conversion function:
    # Define the CelsiusToFahrenheit function:
    CelsiusToFahrenheit <- function(TempCelsius)
    {
     TempFahrenheit <- TempCelsius * (9/5) + 32
     TempFahrenheit
     }
    # Run the function to produce the output
    output <- CelsiusToFahrenheit(TempCelsius = input1)
    Comment: Alternatively, you can also call a built-in function from Spotfire Enterprise Runtime for R.

  9. Select Return type to define the Spotfire data type that the script should return.

    Comment: Make sure that the output from the script matches the selected return type. For example, the script in the example will produce a Real value, hence, Real should be selected as the return type.

  10. Select the Category in which to place the function in the function list.

See also:

How to Edit an Expression Function