# Testing Data Functions Inside and Outside of Spotfire

When you test a data function, to make sure you get consistent results in both the TERR engine in RStudio and the TERR engine in Spotfire, use the same data format and the same TERR engine in both environments.

## About this task

When you read a .csv file containing your data, the TERR or open-source R function `read.table` performs a different data conversion than the conversion performed when you import the same data into Spotfire. The difference in these conversions can cause unwanted differences.

Perform this task in Spotfire Analyst.

## Before you begin

RStudio is installed on your computer.

## Procedure

1. Import the file containing your data into Spotfire.
   Typically, this file is in .csv or a similar format.
   1. From the menu, click File > Open, and browse to the data file.
   2. In the Import Settings dialog, confirm the column data types, and then click OK.
      The data is imported, and a recommended visualization is displayed.
2. From the menu, click File > Export > Data to File.
   The Export Data dialog is displayed.
3. Select Export all rows, and then click OK.
4. Browse to the location to store the exported data, and in the Save as type drop-down list box, select Spotfire Binary Data Format (*.sbdf).
5. From the menu, click Tools > TERR Tools, and then click Launch RStudio IDE.
   RStudio opens, with the configuration to run the TERR engine that is included with Spotfire.
6. At the command prompt, read in the data using the following command.

       myData <- SpotfireData::importDataFromSBDF("myData.sbdf")

   where `myData` is the object name and `myData.sbdf` is the path and the name of the file you saved.
   Note: The path to the file should use forward slashes instead of backslashes.
   The data file is imported into TERR in the same format as is used in Spotfire. This is the same process that is used when data is loaded in the data function.
7. In the RStudio IDE, create and test the function that you plan to use in Spotfire as your data function.
   Tip: If the data function is given zero rows of data (for example, if the data function is configured to be given only marked data, and initially there is often no marked data), in TERR, you can test the function for that possibility by specifying the source of the data as `myData[0,]`. Then testing shows all of the columns of the original data but no rows.
8. Copy the function to use in your data function, and then close RStudio.
9. In Spotfire, register and run the new data function.
10. If you see unexpected results when you run the data function in Spotfire, try the following.
    1. In Spotfire, at the top of the data function, add the following line of code, and then run it.
       `save.image("/winfolder/DataFunction.RData")`
       where winfolder is the Windows folder where to store the .RData file.
    2. In RStudio, at the top of the TERR function, add the following line of code, and then run through each line.
       `load("/winfolder/DataFunction.RData")`
       where winfolder is the Windows folder where to store the .RData file.

       (This loads the data that the data function got from Spotfire.)

## Results

Identify differences; you should have the same results in both environments.