How to call open-source R from TERR to execute R code in open-source R.

How to call open-source R from TERR to execute R code in open-source R.

book

Article ID: KB0080236

calendar_today

Updated On:

Products Versions
Spotfire Enterprise Runtime for R All supported versions

Description

You can use the RinR library in TERR to execute R code in open-source R.  This is helpful if there is R code or a CRAN library which will not run in TERR, but does complete successfully in open-source R.

Issue/Introduction

How to call open-source R from TERR to execute R code in open-source R.

Environment

All supported environments

Resolution

First you will need to load the RinR library in your TERR session, by running the code:

library(RinR)

If you are running this code within a Data Function in Spotfire, you may also need to add the path to the open-source R installation.  This R installation must be located on the same machine as the TERR engine that is being called.  To add the path to open-source R, you can call pushPATH().  For example:

pushPATH("C:/Program Files/R/R-3.4.4/bin")

Now, run the actual REvaluate() call, which contains the R code to execute in R:

Smooth <- REvaluate({
# NOTE, zoo must be installed in the R version you are using
library(zoo)
## create a factor zoo object
fz <- zoo(gl(2,5), as.Date("2004-01-01") + 0:9)
fz <- as.character(fz)
fz

})

The above example does not pass any data from TERR to R, so if you need to pass data, you will need to add another argument named "data" to the REvaluate() call:

Smooth <- REvaluate({
library(zoo)
## create a factor zoo object
fz <- data.frame( date = zoo(gl(2,5), as.Date("2004-01-01") + 0:9), mydata)
fz

}, data = list(mydata = table))

where "table" is the name of the data set in TERR and "mydata" is what it will be called within R when the code is executed.

Additional Information

You can read more about the functions used in the code above by typing a question mark followed by the function name at the TERR command prompt.  For example:

> ?pushPATH
> ?REvaluate

Please note, the RinR package must be loaded (using command 'library(RinR)') before you can access the help files for the functions in the package.