# TERR_Binary

In the Spotfire Custom Expressions dialog box, you can select the pre-defined expression `TERR_Binary` from the Function list. This expression function invokes the TERR engine to return a vector or a single column data frame of the data type `raw`, which is converted to a Spotfire column of the corresponding Spotfire data type Binary.

The expression function has at least two arguments.
Argument | Argument description
--- | ---
A TERR script. | The TERR script contains the following. A number of variables using the naming convention that Spotfire requires: `input1` to `inputN`, where `inputN` is the highest number of the specified inputs, numbered sequentally. A TERR assignment operator (`<-`) that assigns the results of the TERR evaluation to an object named `output` (also using the naming convention that Spotfire requires).
Spotfire column names. | Passed as additional arguments, these are the data column names that `input1` to `inputN` represent. All columns must be the same length.

The output type is returned from TERR and converted by Spotfire.
Returned by TERR | Converted in Spotfire
--- | ---
A vector or a single column data frame of data type `raw`. | A column with the same number of rows as the input, and of the data type Binary.

## TERR_Binary example

In this example, in Spotfire, create a one-column table called Measure containing the numbers between 0 and 5. Next, create a new column called Outliers, which displays the following, based on the number in the Measure column.

- If the number is below 3.5, display nothing.
- If the number is between 3.5 and 4.5, display a binary image of a small red question mark.
- If the number is over 4.5, display a binary image of a small red exclamation mark.

    TERR_Binary("marks <- list(
        ok = NULL,
        # question mark
        hmm = as.raw(c(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00,
              0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x10,
              0x00, 0x00, 0x00, 0x10, 0x08, 0x03, 0x00, 0x00, 0x00, 0x28, 0x2d,
              0x0f, 0x53, 0x00, 0x00, 0x00, 0x27, 0x50, 0x4c, 0x54, 0x45, 0xff,
              0x00, 0x00, 0xff, 0x00, 0x66, 0xff, 0x3a, 0x00, 0xff, 0x3a, 0x90,
              0xff, 0x66, 0x00, 0xff, 0x66, 0xb6, 0xff, 0x90, 0x3a, 0xff, 0x90,
              0xdb, 0xff, 0xb6, 0x66, 0xff, 0xb6, 0xff, 0xff, 0xff, 0xb6, 0xff,
              0xff, 0xdb, 0xff, 0xff, 0xff, 0xbb, 0xaf, 0xf5, 0x2c, 0x00, 0x00,
              0x00, 0x3f, 0x49, 0x44, 0x41, 0x54, 0x18, 0x95, 0x63, 0xe0, 0x41,
              0x03, 0x0c, 0x64, 0x0a, 0x70, 0x31, 0x30, 0x30, 0x30, 0x22, 0x09,
              0x70, 0x33, 0xb1, 0xf2, 0xf0, 0xb0, 0x31, 0x22, 0x04, 0x38, 0x18,
              0xc1, 0xa2, 0x9c, 0xa8, 0x66, 0x70, 0x31, 0xa0, 0x0a, 0x70, 0x31,
              0xb0, 0xa2, 0xd8, 0x02, 0xe7, 0xc3, 0x04, 0xd8, 0x98, 0xd1, 0xdc,
              0xc1, 0xc2, 0x4e, 0xc8, 0xa5, 0x84, 0x55, 0x20, 0x00, 0x00, 0xe8,
              0x14, 0x0b, 0x2c, 0x7d, 0xc8, 0x15, 0x53, 0x00, 0x00, 0x00, 0x00,
              0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82)),
        # exclamation mark
        oops = as.raw(c(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00,
               0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x10,
               0x00, 0x00, 0x00, 0x10, 0x08, 0x03, 0x00, 0x00, 0x00, 0x28, 0x2d,
               0x0f, 0x53, 0x00, 0x00, 0x00, 0x21, 0x50, 0x4c, 0x54, 0x45, 0xff,
               0x00, 0x00, 0xff, 0x00, 0x3a, 0xff, 0x00, 0x66, 0xff, 0x3a, 0x66,
               0xff, 0xb6, 0x66, 0xff, 0xb6, 0xff, 0xff, 0xdb, 0x90, 0xff, 0xdb,
               0xff, 0xff, 0xff, 0xb6, 0xff, 0xff, 0xdb, 0xff, 0xff, 0xff, 0x10,
               0x12, 0xb4, 0x48, 0x00, 0x00, 0x00, 0x28, 0x49, 0x44, 0x41, 0x54,
               0x18, 0x95, 0x63, 0xe0, 0x42, 0x03, 0x0c, 0xe4, 0x0a, 0xb0, 0x30,
               0xb0, 0x12, 0x10, 0x60, 0xc3, 0x10, 0x60, 0x64, 0x27, 0x20, 0xc0,
               0x81, 0x21, 0xc0, 0x84, 0x66, 0x2d, 0x27, 0x33, 0xc9, 0x0e, 0xc3,
               0xe7, 0x17, 0x00, 0x05, 0x3d, 0x09, 0x6f, 0xa9, 0xc3, 0xf3, 0x36,
               0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60,
               0x82)))
        # Use first item in input2 and input3 because they have been extended to length of input1
        whichMark <- findInterval(input1, c(-Inf, input2[1], input3[1]))
        # Wrap output list in I() so it remains one column in data.frame
        output <- I(marks[whichMark])", [Measure], 3.5, 4.5)

The resulting table in Spotfire shows the following.

Important: Spotfire autocorrects the function case or name to that of built-in Spotfire function names. (For example, TERR contains the function `max`, and Spotfire contains the function `Max`. ) You must overwrite this autocorrection manually to ensure that you use the TERR function case and name in your expression function.

See Embedding the Contents of a Script in an Expression Function for a detailed procedure for creating an expression function.