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)

Installation

  1. Install R statistical environment from http://www.r-project.org

  2. Install R package rcproxy from http://cran.rakanu.com/bin/windows/contrib/2.8/rscproxy_1.2-0.zip

  3. Install DCOM Server from http://cran.r-project.org/contrib/extra/dcom/

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
R
      +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
47
      +/x÷⍴x            ⍝ the same in APL
47
      y←rget 'y'        ⍝ get variable y from R to APL
      x≡y               ⍝ compare with original x
1
      y←?100 6⍴2*30     ⍝ generate another random set
      y←(+/y÷2*30)÷6
      'y'rput y         ⍝ put it in R
      rexec 'hist(y)'   ⍝ call R function to plot histogram

And R windows with histogram plot pops up:

histd.png

Dyalog APL functions

rinit

Initialize APL connection to R COM Server.

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

rexec

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

rput

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

rget

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

r2apl.DWS

Author: AlexanderSkomorokhov 2009-04-06


CategoryTechnologies CategoryDyalog

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