Running R scripts on AX Server

Import external R scripts as related files along with an analysis app and then call the R scripts from your analytics to leverage the statistical analysis capabilities of the R scripting language on the server. To prepare the AX Server environment to run R scripts, you must first install R and then add the .r extension to the file extension whitelist.

Prerequisites

To run R scripts on AX Server, you must:

  1. Install a supported version of the R scripting language on your AX Server computer.
  2. Add the .r extension to the file extension whitelist on AX Server.
  3. In ACL Analytics, create a project to work with and import into AX Server.

Note

For help completing these prerequisites, contact your Analytics Exchange administrator and see:

Add R scripts to the ACL project directory

After you create your ACL project in ACL Analytics, copy the R scripts you intend to use and paste them into the project folder so that you can test your script locally in ACL Analytics before importing it to Analytics Exchange.

Example R files

The following example R files contain trivial scripts that concatenate two strings and return a single string joined by a space character. These examples are intended to show how R scripts run on AX Server, not how to analyze data with R.

analysis_a.r

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

analysis_b.r

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

Create an ACL script

In your ACL project, create a new script to use as the analytic you run on AX Server. This script does the following:

  1. Opens a temporary table called t_tmp with one record.

    You must open a table to execute the EXTRACT command in ACL, here the t_tmp table is used only for this purpose.

  2. Uses the EXTRACT command to run each R script and writes the results to a table.

Add the analytic header

Add the appropriate analytic header tags at the beginning of the script so that the ACL script can run on AX Server after you import your analysis app. You must add a FILE tag for any R script you intend to run from the analytic:

COMMENT
//ANALYTIC R integration test 
  verify R integration on AX Server
//DATA t_tmp
//FILE analysis_a.r
//FILE analysis_b.r
//RESULT TABLE results
END

Add the script logic

SET SAFETY OFF
DEL ALL OK
CLOSE PRIMARY SECONDARY

OPEN t_tmp

COM **** execute R scripts and write results to table
EXTRACT FIELDS RSTRING("a<-source('./analysis_a.r');a[[1]]",50,"test","value") AS "value" TO "results.fil"
EXTRACT FIELDS RSTRING("a<-source('./analysis_b.r');a[[1]]",50,"test","value") AS "value" TO "results.fil" APPEND
			
CLOSE t_tmp

The complete analytic script

The complete analytic that you run on AX Server looks as follows:

COMMENT
//ANALYTIC R integration test 
 verify R integration on AX Server
//DATA t_tmp
//FILE analysis_a.r
//FILE analysis_b.r
//RESULT TABLE results
END

SET SAFETY OFF
DEL ALL OK
CLOSE PRIMARY SECONDARY

OPEN t_tmp

COM **** execute R scripts and write results to table
EXTRACT FIELDS RSTRING("a<-source('./analysis_a.r');a[[1]]",50,"test","value") AS "value" TO "results.fil"
EXTRACT FIELDS RSTRING("a<-source('./analysis_b.r');a[[1]]",50,"test","value") AS "value" TO "results.fil" APPEND

CLOSE t_tmp

Import the ACL project and related R files

Once you have authored the analytic script:

  1. In AX Client, create a collection and folder to house the ACL project.
  2. To import the project and R files:
    1. Right-click the folder you created and select Import.
    2. Navigate to your ACL project on your local computer and select the .acl project file and the .r R scripts.

      Note

      Make sure you select the R files in the project folder as well as the ACL project using Ctrl+click so that they are imported into AX Server. You must also import the source data files for the t_tmp table.

    3. Click Open.

Server explorer after import

  • collectionName
    • folderName
      • Analysis Apps
        • ACLProjectName
          • analyticScriptName
      • Data
        • t_tmp
      • Related Files
        • analysis_a.r
        • analysis_b.r

Run the analytic

From the Server Explorer of AX Client, right-click the analytic and select Run. The R scripts are executed as part of the analytic and you can access the results results table from AX Web Client.

Results

Server explorer after running the analytic

  • collectionName
    • folderName
      • Analysis Apps
        • ACLProjectName
          • analyticScriptName
      • Data
        • results
      • Related Files
        • analysis_a.r
        • analysis_b.r

Results table

  • value
  • test value
  • value test

(C) ACL Services Ltd. All Rights Reserved.

Saturday, April 20, 2019