RSTRING( ) function

Returns a string value calculated by an R function or script. Data processing in R is external to Analytics.

Syntax

RSTRING(rScript|rCode, length <,field|value <,...n>>)

Parameters

Name Type Description
rScript | rCode

character

The full or relative path to the R script, or a snippet of R code to run.

If you enter R code directly rather than use an external file, you cannot use the enclosing quotation character in your code, even if you escape it:

  • valid 'var <- "\"test\"" '
  • invalid 'var <- "\'test\'" '
length numeric The length to allocate for the return string.
field | value <,...n>

optional

character

numeric

datetime

logical

The list of fields, expressions, or literal values to use as arguments for the R script or code snippet.

The values are passed into the function you call in the order you specify them, and you reference them using value1, value2 ... valueN in the R code.

You may include as many arguments as necessary to satisfy the function definition in the R code.

Note

Use the ALLTRIM() function to remove any leading or trailing spaces from character input: ALLTRIM(str). For more information, see ALLTRIM( ) function.

Output

Character.

Examples

Basic examples

Returns "abc123":

RSTRING("print(paste(value1,value2,sep=""))",6,"abc","123")

Advanced examples

Using an external R script

Concatenates x and y into a single string delimited by a space character:

RSTRING("a<-source('./sample.r');a[[1]]",50, FirstName, LastName)

External R script (sample.r):

conc <- function(x, y) {
    paste(x, y, sep=" ")
}
print(conc(value1, value2))

Using R code stored in a variable

Concatenates x and y into a single string delimited by a space character:

ASSIGN v_script = "conc <- function(x, y){paste(x, y, sep=' ')};conc(value1, value2)"
RSTRING(v_script, 50, FirstName, LastName)

Using R to generate a UUID for a table

You are preparing a table of exceptions to upload to Results and you require a guaranteed unique identifier for each record. To generate this field, you use the uuid package in R to create a unique primary key value for each record:

EXTRACT RSTRING("uuid::UUIDgenerate()", 36) AS "id", first_name, last_name, birthdate TO export_table

Tip

To install the uuid package, open R.exe and execute the following command:

install.packages("uuid")

Remarks

Returning data from R

When calling R scripts, use the source function and assign the return object to a variable. You can then access the value returned from your R function from the return object:

# 'a' holds the response object and a[[1]] access the data value
"a<-source('c:\\scripts\\r_scripts\\sample.r');a[[1]]"

R log file

Analytics logs R language messages to an aclrlang.log file in the project folder. Use this log file for debugging R errors.

Tip

The log file is available in the Results folder of Analytics Exchange analytic jobs.

Running external R scripts on AX Server

If you are writing an analysis app to run on AX Server and you want to work with external R scripts:

  1. Upload the file as a related file with the analysis app.
  2. Use the FILE analytic tag to identify the file(s).
  3. Reference the file(s) using the relative path ./filename.r.

Note

Using a related file ensures that the TomEE application server account has sufficient permissions to access the file when running R with Analytics Exchange.

ACL Scripting Guide 14.1