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
- After installer finished, launch R
- Type:
Sys.setenv(R_LIBS_USER="C:/Users/«yourName»/AppData/Local/R/win-library/4.3")
- where you replace «yourName» with your login name.
- The setenv step is required because R installs into
"C:\Program Files"
by default, but then new packages can't be installed there, so this puts them into a writable location.
Install packages
This is the hard part.
- 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"
- In a CMD prompt:
CD "C:\Program Files\R\R-4.3.0\bin\x64"
Rcmd install RWinRegistry
- Install the R package Ruuid.
- Start R
install.packages("BiocManager")
- Launch R
- Run:
install.packages("devtools")
devtools::install_github("omegahat/RDCOMServer")
-- I was unable to get R packages to install. As someone who doesn't use R, I'm afraid I don't know enough to get to first base to even get started. Aborted here. Sorry.
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