Interface to R

R is a computer language widely used in statistics. It includes a huge library of statistical and graphical functions which can be called from APL. For example, you can call R to do linear and non-linear modelling, classical statistical tests, time-series analysis, classification, clustering and many other tasks.

You can call the R library through the COM interface. This only works under Windows

The two methods are described below.

Using COM Server (Dyalog)


  1. Install R statistical environment from

  2. Install R package rcproxy from

  3. Install DCOM Server from

Sample APL Session

     )load r2apl
d:\r2apl saved Mon Apr 06 22:10:59 2009
     rinit ⍝ initialize connection to R COM server
     )obs  ⍝ new object R is created
      +x←10?100         ⍝ random numbers in APL
14 76 46 54 22 5 68 94 39 52
      'y'rput x         ⍝ send them to R
      1 rexec 'mean(y)' ⍝ calculate mean value in R
      +/x÷⍴x            ⍝ the same in APL
      y←rget 'y'        ⍝ get variable y from R to APL
      x≡y               ⍝ compare with original x
      y←?100 6⍴2*30     ⍝ generate another random set
      'y'rput y         ⍝ put it in R
      rexec 'hist(y)'   ⍝ call R function to plot histogram

And R windows with histogram plot pops up:


Dyalog APL functions


Initialize APL connection to R COM Server.

     ∇ rinit
[1]    '#.R'⎕WC'OleClient' 'StatConnectorSrv.StatConnector'
[2]    #.R.Init('R')


Execute R expression and possibly return the result to APL

     ∇ a←{r}rexec exp
[1]    ⍝ Execute R expression and possibly return the result
[2]    ⍝R: R expression ('char')
[3]    ⍝L: if 1, then return the result
[4]    ⍎(0=⎕NC'r')/'r←0'
[5]    a←⍬
[6]    :If r
[7]        a←#.R.Evaluate(exp)
[8]    :Else
[9]        #.R.EvaluateNoReturn(exp)
[10]   :EndIf


Put APL array into R

     ∇ {m}rput a;x1
[1]    ⍝ Put APL array into R
[2]    ⍝R-name of variable to export from APL
[3]    ⍝L-name of variable to create in R
[4]    ⍎(0=⎕NC'm')/'m←''x''' ⍝ Default name is x
[5]    m←,⊂m
[6]    a←,⊂a
[7]    #.R.SetSymbol(m,a) ⍝ put var a into R with name m


Get R variable (matrix or vector) to APL

     ∇ r←rget name;rho;Re;Im;⎕ML
[1]   ⍝ Get R variable (matrix or vector) to APL
[2]   ⍝R-name of variable to import ('char')
[3]    ⎕ML←3
[4]    r←#.R.GetSymbol(name)

Get workspace here

You can download a copy of the workspace here


Author: AlexanderSkomorokhov 2009-04-06

CategoryTechnologies CategoryDyalog

r2apl (last edited 2017-02-16 18:39:27 by KaiJaeger)