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 analytic scripts 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:
- Install a supported version of the R scripting language on your AX Server computer.
- Add the .r extension to the file extension whitelist on AX Server.
- In 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 Analytics project directory
After you create your Analytics project in 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 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 Analytics script
In your Analytics project, create a new script to use as the analytic script you run on AX Server. This script does the following:
- Opens a temporary table called t_tmp with one record.
You must open a table to execute the EXTRACT command in Analytics, here the t_tmp table is used only for this purpose.
- 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 Analytics 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 script:
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 script 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 Analytics project and related R files
Once you have authored the analytic script:
- In AX Client, create a collection and folder to house the Analytics project.
- To import the project and R files:
- Right-click the folder you created and select Import.
- Navigate to your Analytics 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 Analytics 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.
- Click Open.
Server explorer after import
- collectionName
- folderName
- Analysis Apps
- ACLProjectName
- analyticScriptName
- ACLProjectName
- Data
- t_tmp
- Related Files
- analysis_a.r
- analysis_b.r
- Analysis Apps
- folderName
Run the analytic script
From the Server Explorer of AX Client, right-click the analytic script and select Run. The R scripts are executed as part of the analytic script and you can access the results results table from AX Web Client.
Results
Server explorer after running the analytic script
- collectionName
- folderName
- Analysis Apps
- ACLProjectName
- analyticScriptName
- ACLProjectName
- Data
- results
- Related Files
- analysis_a.r
- analysis_b.r
- Analysis Apps
- folderName
Results table
- value
- test value
- value test