Interface to R
Contents
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 in two ways:
- If you are running Dyalog or APLX or any other COM-capable APL, you can call R through the COM interface.
- This method only works under Windows
- If you are running APLX Version 5 or later, you can call R directly through APLX's plug-in architecture
- This method works under Windows, Macintosh and Linux and is more tightly integrated, but requires APLX Version 5.
The two methods are described below.
Using COM Server (Dyalog)
Installation
Install R statistical environment from http://www.r-project.org
Install R package rcproxy from http://cran.rakanu.com/bin/windows/contrib/2.8/rscproxy_1.2-0.zip
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 histogramAnd R windows with histogram plot pops up:
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
- Dyalog
- APLX (Equivalent functions to the ones listed above)
Using R from APLX Version 5
APLX includes the ability to call code written in other languages like .NET, Java, Ruby and R through an external plug-in mechanism. This allows a tighter integration of APL and R than you can achieve with the COM interface. For example, it allows support for:
- complex R data types (data frames, factors, time series, etc),
- NA (not-available) values
- attributes
access to the text form of a variable via ⎕DS
Sample APLX Session
⍝ Load the plugin library
r←'r' ⎕new 'r'
r
[r:R]
r.⎕DS
R version 2.9.0 (2009-04-17)
⍝ First some simple stuff...
r.x←10?10 ⍝ APL data assigned to R variable 'x'
r.x
2 8 5 6 3 1 7 10 4 9
r.⎕EVAL 'mean(x)' ⍝ Evaluate arbitrary expression
5.5
r.mean (⊂10?10) ⍝ No need to use an R variable
5.5
r.y←?100 6⍴2*30 ⍝ Generate another random set
r.y←(+/r.y÷2*30)÷6
r.⎕EVAL 'hist(y)' ⍝ Call R function to plot histogram
[r:histogram] ⍝ (Shows the same histogram as in the
⍝ Dyalog example above)
⍝ Let's create an R time series...
ts←r.ts (⊂12?100)
ts
[r:ts]
ts.⎕DS ⍝ Examine text form
Time Series:
Start = 1
End = 12
Frequency = 1
[1] 93 94 45 53 84 1 63 75 56 54 25 98
ts.⎕val
93 94 45 53 84 1 63 75 56 54 25 98
⍝ R data's attributes can be accessed and changed
⍝ using the ∆XXX syntax...
ts.attributes.⎕DS ⍝ Get the attributes
$tsp
[1] 1 12 1
$class
[1] "ts"
ts.∆tsp
1 12 1
ts.∆tsp←2008 (2008+11÷12) 12 ⍝ Change the start/end time and frequency
ts.⎕DS
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2008 93 94 45 53 84 1 63 75 56 54 25 98
⍝ An example of a data frame...
⍝ Get the built-in data set giving eruption data for the Old Faithful geyser
r.faithful
[r:frame]
⍝ Examine the data as text
200 ↑r.faithful.⎕DS
eruptions waiting
1 3.600 79
2 1.800 54
3 3.333 74
4 2.283 62
5 4.533 85
6 2.883 55
7 4.700 88
8 3.600 85
9
⍝ Get a named column from the data frame
eruptions←(r.faithful).$$ 'eruptions'
10↑eruptions
3.6 1.8 3.333 2.283 4.533 2.883 4.7 3.6 1.95 4.35
⍝ Could also do
eruptions← ⊃ r.faithful.⎕val[1]
⍝ Plot a histogram of the eruption durations in minutes
r.eruptions←eruptions
r.⎕EVAL 'hist(eruptions,seq(0,6,0.2),prob=TRUE)'
[r:histogram]
⊣ r.⎕EVAL 'lines(density(eruptions,bw=0.1))'
⍝ For eruptions of more than 3 minutes' duration,
⍝ apply Kolmogorov-Smirnov test to check whether duration
⍝ obeys a normal distribution...
r.long←(eruptions > 3)/eruptions
ks←r.⎕EVAL 'ks.test (long, "pnorm", mean=mean(long), sd=sd(long))'
ks
[r:htest]
ks.⎕DS
One-sample Kolmogorov-Smirnov test
data: long
D = 0.0661, p-value = 0.4284
alternative hypothesis: two-sided
ks.⎕VAL
0.06613335935 0.4283591902 two-sided One-sample Kolmogorov-Smirnov testAuthor: AlexanderSkomorokhov 2009-04-06
APLX material: SimonMarsden 2009-06-30
APL Wiki