Initial Release
This repository is a template for using the new R gateway for IRIS 2021.1.
The architecture of this template is as follows:
It is composed of three containers, with one for:
Iris and the R server will discuss through the java gateway.
These first two containers are bound to the data/
local folder, and so have access to the same data:
This file is loaded as a table in IRIS :
The R container also has access to the r/
folder, and in it is the following script:
getMode <- function(x) {
l <- unique(x)
l[which.max(tabulate(match(x, l)))]
}
getLength <- function(x) {
length(x)
}
createHistJPG <- function(data, dir, label) {
dir2 = paste(dir, "/hist.jpeg", sep="")
jpeg(file = dir2)
hist(data, xlab=label)
dev.off()
}
createHistPNG <- function(data, dir, label) {
dir2 = paste(dir, "/hist.png", sep="")
png(file = dir2)
hist(data, xlab=label)
dev.off()
}
createHistPDF <- function(data, dir, label) {
dir2 = paste(dir, "/hist.pdf", sep="")
pdf(file = dir2)
hist(data, xlab=label)
dev.off()
}
git clone https://github.com/grongierisc/iris-r-gateway-template
then :
docker compose up
Run the following ObjectScript commands
do ##class(Demo.RGateway).RunEval()
output :
"R version 3.4.4 (2018-03-15)"
code :
ClassMethod RunEval() As %Status
{
// Get a gateway instance
set gateway = ##class(%Net.Remote.Gateway).%New()
set tSC = gateway.%Connect(..#JGWHOST, ..#JGWPORT)
if $$$ISERR(tSC) quit
// Bind the gateway to the Rserve
set c = ##class(%Net.Remote.Object).%ClassMethod(.gateway,"com.intersystems.rgateway.Helper","createRConnection", ..#RHOST, ..#RPORT)
// Run a random R command
zw c.eval("R.version$version.string").asString()
<span class="pl-k">do</span>:<span class="pl-v">c</span>'=<span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span> <span class="pl-v">c</span>.<span class="pl-e">close</span>()
<span class="pl-k">do</span>:<span class="pl-v">gateway</span>'=<span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span> <span class="pl-v">gateway</span>.<span class="pl-e">%Disconnect</span>()
<span class="pl-k">quit</span>
}
do ##class(Demo.RGateway).FromSQLOnIris()
output :
"mean = 0.5239920995930093"
"median = 0.545"
code :
ClassMethod FromSQLOnIris()
{
set gateway = ##class(%Net.Remote.Gateway).%New()
set tSC = gateway.%Connect("r",55554)
if $$$ISERR(tSC) quit
<span class="pl-k">set</span> <span class="pl-v">c</span> = <span class="pl-k">##class</span>(<span class="pl-en">%Net.Remote.Object</span>).<span class="pl-e">%ClassMethod</span>(.<span class="pl-e">gateway</span>,<span class="pl-s"><span class="pl-pds">"</span>com.intersystems.rgateway.Helper<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>createRConnection<span class="pl-pds">"</span></span>)
// SQL Query
set q = "select length from Test.Data"
set resultSet = ##class(%SQL.Statement).%ExecDirect( , .q)
// Prepare R vector
do c.eval("l = c()")
while resultSet.%Next() {
// Append resultSet to the R vector
do c.assign("x", ##class(%Net.Remote.Object).%New(gateway, "org.rosuda.REngine.REXPDouble", resultSet.Length))
do c.eval("l = append(l, c(x))")
}
// Produce mean from R
zw "mean = " _c.eval("mean(l)").asString()
// Produce median from R
zw "median = "_c.eval("median(l)").asString()
<span class="pl-k">do</span>:<span class="pl-v">c</span>'=<span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span> <span class="pl-v">c</span>.<span class="pl-e">close</span>()
<span class="pl-k">do</span>:<span class="pl-v">gateway</span>'=<span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span> <span class="pl-v">gateway</span>.<span class="pl-e">%Disconnect</span>()
<span class="pl-k">quit</span>
}
do ##class(Demo.RGateway).FromCSVOnRServe()
output :
"mean = 0.5239920995930093"
"median = 0.545"
"mode = 0.55"
"plot produced in /tmp/data/hist.png"
"plot produced in /tmp/data/hist.jpeg"
"plot produced in /tmp/data/hist.pdf"
code :
ClassMethod FromCSVOnRServe()
{
set gateway = ##class(%Net.Remote.Gateway).%New()
set tSC = gateway.%Connect(..#JGWHOST, ..#JGWPORT)
if $$$ISERR(tSC) quit
<span class="pl-k">set</span> <span class="pl-v">c</span> = <span class="pl-k">##class</span>(<span class="pl-en">%Net.Remote.Object</span>).<span class="pl-e">%ClassMethod</span>(.<span class="pl-e">gateway</span>,<span class="pl-s"><span class="pl-pds">"</span>com.intersystems.rgateway.Helper<span class="pl-pds">"</span></span>,<span class="pl-s"><span class="pl-pds">"</span>createRConnection<span class="pl-pds">"</span></span>, ..#RHOST, ..#RPORT)
// Read a file from R
// assign an R variable for the data file
set filename = "/tmp/data/abalone.csv"
do c.assign("filename", filename)
// Read the file from the R server
do c.eval("data = read.csv(filename)")
<span class="pl-k">zw</span> <span class="pl-s"><span class="pl-pds">"</span>mean = <span class="pl-pds">"</span></span>_<span class="pl-v">c</span>.<span class="pl-e">eval</span>(<span class="pl-s"><span class="pl-pds">"</span>mean(data$Length)<span class="pl-pds">"</span></span>).<span class="pl-e">asString</span>()
<span class="pl-k">zw</span> <span class="pl-s"><span class="pl-pds">"</span>median = <span class="pl-pds">"</span></span>_<span class="pl-v">c</span>.<span class="pl-e">eval</span>(<span class="pl-s"><span class="pl-pds">"</span>median(data$Length)<span class="pl-pds">"</span></span>).<span class="pl-e">asString</span>()
// Call function from R script
// assign an R variable for the script file
do c.assign("rFile", "/tmp/r/src/test.R")
do c.eval("source(rFile)")
<span class="pl-k">zw</span> <span class="pl-s"><span class="pl-pds">"</span>mode = <span class="pl-pds">"</span></span>_<span class="pl-v">c</span>.<span class="pl-e">eval</span>(<span class="pl-s"><span class="pl-pds">"</span>getMode(data$Length)<span class="pl-pds">"</span></span>).<span class="pl-e">asString</span>()
// Plot graphs
// assign a directory to save files
do c.assign("dir", "/tmp/data")
// produce plot PNG
do c.eval("createHistPNG(data$Rings, dir, ""Rings"")")
zw "plot produced in /tmp/data/hist.png"
// produce plot JPG
do c.eval("createHistJPG(data$Rings, dir, ""Rings"")")
zw "plot produced in /tmp/data/hist.jpeg"
// produce plot PDF
do c.eval("createHistPDF(data$Rings, dir, ""Rings"")")
zw "plot produced in /tmp/data/hist.pdf"
<span class="pl-k">do</span>:<span class="pl-v">c</span>'=<span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span> <span class="pl-v">c</span>.<span class="pl-e">close</span>()
<span class="pl-k">do</span>:<span class="pl-v">gateway</span>'=<span class="pl-s"><span class="pl-pds">"</span><span class="pl-pds">"</span></span> <span class="pl-v">gateway</span>.<span class="pl-e">%Disconnect</span>()
<span class="pl-k">quit</span>
}
You can have a look at this R Gateway for IRIS :
https://github.com/intersystems-community/RGateway
This one doesn't use the java gateway and use a direct connection between IRIS and R Server.
Exemple :
Set c = ##class(R.RConnection).%New() // Create a R client
Set x = ##class(R.REXPDouble).%New(3.0) // A single double value
Do c.assign("x", x) // Assign the value to R variable x
Do c.eval("y<-sqrt(x)") // Evaluate R script
Set y = c.get("y") // Get the value of R variable y
Write y.toJSON().%ToJSON()
Initial Release