RNUMERIC( ) function

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

Syntax

RNUMERIC(rScript|rCode, decimals <,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\'" '
decimals numeric The number of decimal places to include in the return value. Must be a positive integer.
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

Numeric.

Examples

Basic examples

Returns 100 with 10 decimals (100.0000000000):

RNUMERIC("print(value1)", 10, 100)

Advanced examples

Storing R code as a variable

Returns 100 with 10 decimals (100.0000000000):

ASSIGN v_rcode = "print(value1)"
RNUMERIC(v_rcode, 10, 100)

Writing to an external file

Performs a simple addition and writes the comment attached to the function to file using the sink function in R:

RNUMERIC("foo<-function(x,y){x+y};attr(foo, 'comment') <- 'foo performs simple addition';sink('c:/temp/result.txt');attributes(foo);sink(NULL);foo(value1,value2)",0, amt, gross)

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.