Using the R Process Definition Type

The R language, an offshoot of S, is a statistical and graphics language that runs on all three major platforms: Windows, Linux, and macOS.

Note: You must assign at least one Process Server to run R processes in order to use the Process Definition Type.

Overriding the Interpreter

The standard interpreter used is the one you get by calling R. On Windows, if the R binary is not on the path, it will be located via the registry. This should work fine on all platforms, but if it doesn't, you can override it as follows.

  • JCS_INTERPRETER_R: Set this value to override the default interpreter. It can be set to any value allowed by InterpreterWhitelist_R.
  • InterpreterWhitelist_R: A Process Server parameter that takes a regular expression matching all interpreters you want supported. Use .* to allow any. On Windows, a common setting is C:/Program Files/R/.*/R.exe to allow any version to be used.
  • LocalInterpreter_R: A Process Server parameter used to set the default interpreter.

Variables and Parameters

You can manipulate Process Definition parameters in R scripts as if they are scalars or vectors, using the standard syntax. The script runs in a fresh environment, so there is no danger of a clash with object names used by the Platform Agent runner.

String, character, and Numeric parameters are translated to their equivalent R data types.

Array parameters are supported and converted to vectors. All elements must be unique.

Note: Date, Time, DateTime, and DateTimeZone (array) parameters are translated to POSIXlt parameters. Because R cannot perform calculations on POSIXlt values that are not in its local timezone, DateTimeZone values are translated to the local timezone. This means that you cannot transfer the time zone of a parameter to a R variable. It will be converted to the corresponding date and time in the local time zone of the host system. Out parameters will keep the time zone created for them.

Error Handling

If your script code exits with an error, this is correctly reflected in RunMyJobs. Output parameters are set as far as they were already set. The R process sets .Last and options(error) to accomplish this, so if you override these, then output parameters will not be set if the process aborts with an error or quits without calling .Last.

Character Set

On Windows, the character set used for the script and the parameter transfer is the local default character set for Windows. This can be the standard 1252 code page used by most Windows systems, or any other code page set as the default on the system.

On UNIX, the character set is the character set used for the network-processor process. The default may be 'C' or some other setting that does not recognize every character required. On such systems, Redwood recommends setting the LANG environment variable to a UTF-8 character set (for example, en_US.UTF-8).

Examples

The following example shows how to pass numeric (N1), string (S1) and date (D1) parameters to and from R scripts.

Copy
N1    = N1 + N1                             # Add number to itself
S1    = paste(S1, S1)                       # Concatenate string
D1    = D1 - as.difftime(30, units="days")  # Deduct 30 days from D1

# Create a new DateTimeZone output array with 2 different timezones
POUTDTZ = as.POSIXlt(character(0))
POUTDTZ[1] = as.POSIXlt("2023-07-27 01:00:00", tz="WET")
POUTDTZ[2] = as.POSIXlt("2111-11-11 01:00:00", tz="EET")

# Create a new Numeric output array with 7 elements (and one 'not available' value)
PNUMARR=c(1,2,3,4,5,NA,7)

# Create a new String output array with 6 elements (one of which is 'not available')
POUTSTR=c("You", "would", "like", NA, "to", "know")

This example shows how to return a non-zero exit code, resulting in the process going into Error status.

Copy
quit(status = 5)