Statistical computations from Apama Using R Language

Introduction :

SoftwareAG Apama allows statistical computing with R using the R-plugin. R is a free software environment and programming language for statistical computing. R is widely used among stastisticians and Data Scientists for data analysis and graphics. One of R’s strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formulae where needed.

Prerequisites :

1) Download and Install R from https://cran.r-project.org/bin/windows/base/

2) Download and Install R Studio from https://www.rstudio.com/products/rstudio/download/#download

3) Install Apama and Designer from SoftwareAG Installer

Configuring RServe:

1) Add this path to Windows path environment variable. This will allow the Rserve program to locate R.dll.

    C:\Program Files\R\R-3.5.0\bin\x64

2) Click R x64 3.2.0 (or similar) to launch the R Console.

3) Rserve is a TCP/IP server which allows other programs to use facilities of R (see www.r-project.org) from various languages without the need to initialize R or link against R library.

In the R Console, type the following commands in order to download and install Rserve (this is the R server) and run the program.

install.packages("Rserve")
library(Rserve)
Rserve()

Adding the R Support bundle to your Project :

In order to use the R plug-in, user has to only add the R Support EPL bundle to the Apama project.  Including the R Support bundle to Apama project makes the RPlugin.mon file available to the monitors in the Apama Project The file RPlugin.mon acts as interface between the monitors in Apama project and R plug-in.

Steps for using the R Plug-in :

Once the R Support bundle has been added to the Apama project, User can add the following steps while creating the monitor file :

1) Create an RConnection event using the static open() or openDefault() action in the RFactory event with the known host and port.

    com.apama.r.RConnection r:= com.apama.r.RFactory.open("localhost", 6311);

*RServe runs on port 6311

2) Use the set() action in RConnection to set named variables in R. The set() action uses the any type, which means that all types can be passed into it. for eg., r.set ("f",98.768);

3)Use the evaluate() action to evaluate an R expression and return the result. This action returns an any type and the user must process the result to the correct type.

for eg., float fResult := r.evaluateAsFloat("f");

4) Close the connection using close().

Create your first R Program :

Open the R Studio, create your first R program and Run it.

Running the Apama and R file :

1) Open the Designer. Place the newly created mon file and R program in the same directory

2)Load the R Script for eg., r.evaluateVoid("source(file.path(Sys.getenv('APAMA_HOME'),'samples','monitorscript','RPlugin','MyFirstRscript.R'))");

3) Run function from loaded Script for eg., r.evaluate()