Running R scripts

Concept Information

RCOMMAND command

Analyze an Analytics table in an external R script and then return data from R to create a new table in the Analytics project. Source data is passed to R as a data frame that you can reference using a provided function.

Working with Analytics data in R

If you are preparing the R script to run from Analytics, familiarize yourself with the ways data is passed back and forth between Analytics and R. You must use the R functions provided by Analytics in your R script to successfully run the RCOMMAND.

Run an R script

  1. From the menu, select Analyze > R.

    The RCOMMAND dialog box opens.

  2. Next to the R Script field, click Browse and navigate to the R script on your computer that you want to run.
  3. Click Select Fields and add one or more fields to include in the data frame that Analytics makes available in the R script.

    Tip

    You can also include expressions as fields in the data frame. To create an expression, click Expr and use the functions, fields, and operators available to you in the dialog box. For more information, see Expression Builder overview.

  4. Optional. In the RCommand Options section, define how you want to send the Analytics data to the R script.

    For more information, see RCommand options.

  5. Optional. To filter the records that are sent to the R script, click If and use the Expression Builder dialog box to create a conditional expression to use as the filter.

    For more information about creating expressions using the Expression Builder, see Creating expressions using the Expression Builder.

  6. To specify the output table, click To and in the File name field, enter a name for the table and associated .FIL file.

    Use the folder explorer to navigate to the folder you want to use to store the source data file.

  7. Optional. On the More tab of the dialog box, specify any scope options for the command.

    For more information, see More tab.

  8. To run the command, click OK.

RCOMMAND dialog box options

RCommand options

Option Description
Export with field names Use the column titles of the source Analytics table as header values for the R data frame. This option sets KEEPTITLE option on the command and is required if you want to retrieve data using column names in the R script.
Column Separator The character to use as the separator between fields when sending data to R.
Text Qualifier

The character to use as the text qualifier to identify field values when sending data to R.

More tab

Option Description
All Processes all records in the view (default selection).
First Processes from the first record in the table and includes only the specified number of records.
Next

Processes from the currently selected record in the table and includes only the specified number of records.

Note

The number of records specified in the First or Next options references either the physical or the indexed order of records in a table, and disregards any filtering or quick sorting applied to the view. However, results of analytical operations respect any filtering.

If a view is quick sorted, Next behaves like First.

Caution

There is a known issue in the current version with Next when running the RCOMMAND. Avoid using this option as the record reference may reset to the first record regardless of which record is selected.

While

Uses a WHILE statement to limit the processing of records in the primary table based on criteria.

Records in the view are processed only while the specified condition evaluates to true. As soon as the condition evaluates to false, the processing terminates, and no further records are considered. For more information, see Creating expressions using the Expression Builder.