/rsconnect

R connection for Dyalog APL with Rserve.

Primary LanguageAPLMIT LicenseMIT

RSconnect - R connection for DyalogAPL with Rserve

"Rserve (see www.rforge.net/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. Every connection has a separate workspace and working directory." This is true for Linux and Mac but "Windows lacks important features that make the separation of namespaces possible, therefore Rserve for Windows works in cooperative mode only, that is only one connection at a time is allowed and all subsequent connections share the same namespace."

Installation of R in Windows

The easiest way to install R in Windows is to use the following command in terminal

winget install RProject.R

Installation of Rserve

The best way to install Rserve is to install it inside of R.

install.packages("Rserve")

In the default installation the folder of R is C:/Program Files/R/R-4.3.3/ and you should specify it in a settings.json file

"r": {
    "home": "C:/Program Files/R/R-4.3.3/bin/x64/",

and you should also specify the installation folder of Rserve

  "rserve":{
    "home": "C:/Users/kimmo/AppData/Local/R/win-library/4.3/Rserve/libs/x64/",

Use in Dyalog

]load rserve.dyalog
or
2 ⎕FIX 'file://rserve.dyalog' 

Then you should start a Rserve TCP/IP server. You can do by using the command

RS.start

It starts Rserve with a R script and then you are ready to go

r⎕NEW #.RS.Rserve
  r.eval 'Hello World!'
Error 1
  r.eval '"Hello World!"'
Hello World!

At this point there is only two functions in use eval for evaluation and set for setting.

  ar.eval'iris'
  'out' r.set a
1
  a.value(r.eval 'out').value
1

You can find examples in a rsconnect_test.md file. It includes basically all the same examples than in a rconnect (rscproxy-connection.)

A main element in RSconnect is robject. The tool reads R-structures to Robject and each Robject has data-field and value(default) and attributes properties. I use lowercases because of R.

  r.eval 'iris'
#.[Rserve].[robject]
  (r.eval 'iris')[3;]
 (r.eval'iris')[3;]
┌────────────┬───────────┬────────────┬───────────┬───────┐
│Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies│
├────────────┼───────────┼────────────┼───────────┼───────┤
│5.13.51.40.2setosa │
├────────────┼───────────┼────────────┼───────────┼───────┤
│4.931.40.2setosa │
└────────────┴───────────┴────────────┴───────────┴───────┘

You can create robjecct quite easily by yourself and then use R for example for summaries.

mydf⎕new #.RS.robject
mydf.attributes['class' 'names']'data.frame' ('xx' 'square')
mydf.data[1]((12).*1 2)
'mydf' r.set mydf 
1
  mydf.attributes[]
┌─────┬─────┐
│namesclass│
└─────┴─────┘
  mydf.attributes['class']
┌──────────┐
│data.frame│
└──────────┘
  (r.eval 'summary(mydf)').value
┌───────────────┬────────────────┐
│      xxsquare      │
├───────────────┼────────────────┤
│Min.   : 1.00Min.   :  1.00  │
├───────────────┼────────────────┤
│1st Qu.: 3.751st Qu.: 14.25  │
├───────────────┼────────────────┤
│Median : 6.50Median : 42.50  │
├───────────────┼────────────────┤
│Mean   : 6.50Mean   : 54.17  │
├───────────────┼────────────────┤
│3rd Qu.: 9.253rd Qu.: 85.75  │
├───────────────┼────────────────┤
│Max.   :12.00Max.   :144.00  │
└───────────────┴────────────────┘
  (r.eval 'summary(mydf$xx)').value
┌────┬───────┬──────┬────┬───────┬────┐
│Min.1st Qu.MedianMean3rd Qu.Max.│
├────┼───────┼──────┼────┼───────┼────┤
│13.756.56.59.2512  │
└────┴───────┴──────┴────┴───────┴────┘
  (r.eval 'summary(mydf$xx)').data
1 3.75 6.5 6.5 9.25 12

Settings

You can change address and port for Rserve in the settings.json file. These are affecting for server and client side. Presently I assume that that you are using local rserve.

 "rserve":{
    "home": "C:/Users/kimmo/AppData/Local/R/win-library/4.3/Rserve/libs/x64/",
    "address": "localhost",
    "port": 6311,
    "timeout": 2000
  },

The tool uses a R script to start the Rserve in Linux by using

library(Rserve)
Rserve(args="")

This is the easiest way because Rserve() command knows how to find Rserve, how to setup the environment and how to start it, regardless of your platform.

But you should specify the location of Rserve.exe in settings.json file for Windows.