Calling an R function
R is an open source programming language commonly used for data analysis. This page describes how to call an existing R function from a model running in the Analytica Enterprise edition.
Using the RDCOMServer package
One approach of many is to use the RDCOMServer package in R.
Installation of R
- Install R
- Install the RDCOMServer package for R
- Download the Windows Binary ZIP file
- Unzip it into
%R_HOME%\library
. For me this was"C:\Program Files\R\R-4.3.0\library"
- Install the SWinRegistry package.
- Download the Windows Binary ZIP file
- Unzip it into
%R_HOME%\library
. For me this was"C:\Program Files\R\R-4.3.0\library"
- Run:
Rcmd install RWinRegistry
- Install the R package Ruuid.
- Start R
install.packages("BiocManager")
Install packages
- Launch R
- Run:
install.packages("devtools")
devtools::install_github("omegahat/RDCOMServer")
-- This results in an error. RDCOMServer not yet successfully installed. This part is being researched.
Server-side code
- Note: This part is untested, since I haven't yet been able to successful install the required R packages. Work in progress.
Here's example R-code that creates a simple COM server with a single method named multiply:
library(RDCOMServer) serverDesc <- COMCreate("R.Server") serverDesc$setClass("R.Server") # Create the `multiply` method serverDesc$addMethodDef( functionName = "multiply", args = c("x" = "numeric", "y" = "numeric"), return = "numeric", def = function(self, x, y) x * y ) # Register the server serverDesc$register() # Start the server COMServer()
Analytica-side call
- Create the variable:
- Variable RServer ::=
COMCreateObject("R.Server")
- Note: Launch the R-side server before evaluating
RServer
- Variable RServer ::=
- Call the method within a different variable:
- Variable Y ::=
RServer->multiply(5,7)
- Variable Y ::=
Comments
Enable comment auto-refresher