R Script

This article applies to P2 Server version 4.5.4 and later, and P2 Sentinel 4.1.3 and later.

R is an open source programming language and software environment for statistical computing and graphics that is supported by the R Foundation for Statistical Computing.

The R Script component allows you to access R, as a way of analysing data within your process.

Users of the R Code Block should already be familiar with the R Language and syntax, and should refer to the existing R documentation for further help.

The R Script component sends input data to R for processing, using a defined R script. The resultant value declared by the ‘output’ symbol is sent to the ‘out’ output pin. Data which is sent to the input pins is set in R using the Input Symbol name. Each symbol is set to an R dataframe with 2 columns, ‘value’ and ‘timestamp’, where ‘value’ is the value of the datapoint and ‘timestamp’ is the timestamp of the datapoint. The format of the timestamp can be specified in the ‘Timestamp Format’ option.

Once an R Script has finished executing, the Global Environment is cleared. This means that any state which is stored in the Global Environment is lost between each run of the script.

For security reasons, the following R functions are not allowed to be used in Scripts: system, system2, shell, library, require, detach, install.packages, eval, source.

Users who wish to use extra libraries, should contact their System Administrator to install the library into R and also add the library name to the RLibraries configuration setting.


Properties

The R Script component has four properties: Name, R Input Symbols, Timestamp Format, and R Script.

Name

Give the R Script component a name, to differentiate it from other R Script components in the process.

In our example, the Name is ‘R1’.

R Input Symbols

Use the R Input Symbols to add and name data inputs to the R Script. These can then be used in the R Script.

In our example, we’ve added ‘First’ and ‘Second’ as the R Input Symbols. As they are added as properties, they are assigned as inputs to the component.

Timestamp Format

To pass the timestamps of the data points as text, supply a formatting string here. For example, dd/M/yyyy H:mm:s

For details on the format string syntax, please refer to: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings.

If you require the data points to have timestamps passed to R as numeric Posix time (number of seconds since 1/1/1970 at 00:00 UTC), leave the format string blank.

R Script

The R Script should use the R Input Symbols in a script that uses the R Language. You can type the R Script in, or you can load it from an R file that is stored in a file directory.

In our example, the R Script is: ‘output <- First$value + Second$value’. Note that R Language syntax is used here.

  • To edit the R Script, click the Edit R Script…button.

This opens the Edit R Script dialog.

OK or Cancel

  • Click OK to validate and save changes to the script or click Cancel to lose any changes and close the dialog. If the script is not valid, an error dialog will be displayed and the script cannot be saved.

Load R Script…

  • Click Load R Script to load the R Script from an R file stored in a file directory. This overwrites any text that’s in the R Script edit box.

Save R Script…

  • Click Save R Script to save the script from the R Script edit box to a new R file.

Show Installed R Packages…

  • Click Show Installed R Packages… to view the list of all the R packages which are installed into R.


Inputs

The input has a flow, and any defined R Input Symbols.

Flow

The component that acts as a trigger for this component should be connected to this pin.

[R Input Symbol 1]

Define the first R Input Symbol in the component’s Properties. Connect input data to this pin.

[R Input Symbol 2] 

Define the second R Input Symbol in the component’s Properties. Connect input data to this pin.

[R Input Symbol n] 

Define the last R Input Symbol in the component’s Properties. Connect input data to this pin.


Outputs

out 

A mixed value. This is the output value from the R Script’s processing from the ‘output’ symbol set in the R script. Not connecting this pin will produce a warning. If the ‘out’ pin is connected to a DataSet pin (green pin colour), then the data is passed along as a DataSet (Array) otherwise it is passed along as a single value. If the data is passed as a single value but the ‘output’ symbol is an array, the first element of the array is passed.

noData 

If the ‘output’ symbol is not declared in the R Script, the ‘output’ symbol is set to NULL or an array of zero length is returned, and the ‘noData’ pin is followed.

err 

If this pin is not connected, any errors which occur while processing the R script are raised as errors and the processing is halted. If this pin is connected, the error is caught, the ‘err’ pin is followed, and processing continues.


Example

In the following example, the process checks whether the R Script output (the sum of the First and Second values) is greater than a constant (10). If True, a True event is raised; if False, a False event is raised.

The R Script in the R Script component used in this example is: ‘output <- First$value + Second$value’.

Related: Sentinel Studio PrimersCreating a User Process, Usage Examples


Configuration Options

The following settings in the Sentinel Configuration file apply to the R Language component. 

MaximumREngines This setting specifies how many R calculations can be performed concurrently, from Sentinel. R calculations are queued if necessary. The default value in the configuration file is 8. Users should be aware that long-running R scripts may impact the performance of all monitors which are using R scripts. It is only possible to perform processing on multiple R scripts up to the value set by ‘MaximumREngines’. Increasing this value will also increase the total CPU required and so may not increase the total performance of the processing due to the CPU reaching 100% utilisation.
RHostUserName The username which the out-of-process R Hosts are running under. Leaving this blank will run the process as the currently logged in User. Default: (blank)
RHostPassword The password for the ‘RHostUserName’ which the out-of-process R Hosts are running under. Default: (blank)
RLibraries A comma-separated list of R libraries which determines what R functions are pre-loaded in the R Code Block. Default: (blank)
RIllegalFunctions A comma-separated list of R function which are not allowed to be used in R Scripts. Default: (blank)

 

One Comment:

  1. Support for R in P2 Sentinel began in version 4.1.3.

Comments are closed