

Interfacing to the R statistical language 

What is R?R is an opensource language and set of packages aimed principally at statistical analysis. It includes a huge library of prewritten statistical and mathematical routines, which can be accessed immediately and very conveniently from APLX. It also includes mathematicallyoriented graphing facilities. R is available from http://www.rproject.org, which describes R as follows:
Installing RR can be downloaded either in source code form, or as a precompiled binary for most popular platforms, from a number of wesbites (see http://www.rproject.org). In each case you need the R shared library (called libR.so in Linux, R.dll under Windows, and libR.dylib under MacOS); this is usually available in the precompiled binaries. If installing from source, be sure to specify the option enableRshlib when running the configure script. Installing under WindowsThis is most easily done using the installer provided with the prebuilt binaries. The only additional step which you might need to take is to add the R binary directory to your search path, so that APLX can find the DLL R.dll. Installing under Linux and MacOSFollow the instructions provided with the R download. You also need to set up environment variables for R; this is usually done in the R script. Calling R from APLXMost of the interface between APLX and R is done using a single external class, named 'r', which represents the R session that you are running. (Note that this is different from most of the other external class interfaces, where objects of many different classes can be created separately from APLX). You create a single instance of this class using For example: ⍝ Open the R interface and try a few simple things r←'r' ⎕new 'r' r.sqrt 2 1.414213562 r.sqrt (⊂⍳5) 1 1.414213562 1.732050808 2 2.236067977 r.sqrt ¯1 [r:NAN] ⍝ Returns a special R object NAN r.mean (⊂⍳10) 5.5 When calling R functions, the APLX right argument is always a vector where each element corresponds to one argument of the R function. The calls to the sqrt and mean functions above illustrate this; to pass an array as the argument, it needs to be enclosed. Creating variables in the R environmentAssigning to a symbol as though it were a property of the R session class creates a variable in the R world: r.x←2 3⍴⍳6 ⍝ x is an R variable r.x 1 2 3 4 5 6 r.x.⎕ref [r:matrix] Evaluating R expressionsBecause R is an interpreted language, it is possible to use the System Function 'r' ⎕eval '4:9' 4 5 6 7 8 9 However, a more convenient syntax is provided (for the 'r' class only) in which The right argument is a text vector containing any expression which is a valid line of R code. The result is the explicit result (if any) of evaluating the expression in the external environment. For example: r←'r' ⎕new 'r' r.x←2 3⍴⍳6 ⍝ x is an R variable r.x 1 2 3 4 5 6 r.⎕eval 'x[2,]' 4 5 6 r.⎕eval 'mean(x[2,])' 5 Note that the last line could be executed using the alternative syntax where 'r' ⎕eval 'mean(x[2,])' 5 Example: 3D plotIn this short but complete example (based on an article by Skomorokhov and Kutinsky from Quote Quad 123 No 4), we create some data in the R environment, define an R function, and run the R outer product to create some test data. We then call the R persp function to create a 3D plot: r←'r' ⎕new 'r' x←r.⎕eval 'seq(10,10,length=50)' y←x ⍝ Define an R function and return a reference to it: fn←r.⎕eval 'foo<function(x,y){r<sqrt(x^2+y^2);10*sin(r)/r}' fn [r:function] r.z←r.outer(x y fn) r.x←x r.y←y ⊣r.⎕eval 'persp(x,y,z,theta=30,phi=30,expand=0.5,xlab="X",ylab="Y",zlab="Z")' This causes R to open a window and display a 3d perspective chart: Listing R variables and functionsThe ⍝ List R variables: vars←r.⎕nl 2 ⍴vars 129 21 ⍝ List R functions: fns←r.⎕nl 3 ⍴fns 2058 34 ⍝ There are lots of them!
fns2←r.⎕desc 3 fns2[1445+⍳5;] pwilcox (q, m, n, lower.tail = TRUE, log.p = FALSE) q (save = "default", status = 0, runLast = TRUE) qbeta (p, shape1, shape2, ncp = 0, lower.tail = TRUE, log.p = FALSE) qbinom (p, size, prob, lower.tail = TRUE, log.p = FALSE) qbirthday (prob = 0.5, classes = 365, coincident = 2) R naming conventionsR function names can have characters such as a < and  in them, which are not legal as symbol names in APLX. To call these in APLX as direct method calls, you need to escape the illegal character with a $ character. (This is not of course necessary when using For example, to call attr< from APLX, you would call r.attr$<$. Conversion of R data types to APL dataSimple numeric arrays and arrays of strings passed from APLX to R are converted directly to the R equivalent array, and are converted back automatically ('unboxed') when referenced or returned from an R function call, unless you use r.y←2.2 3.3 4.4 r.y 2.2 3.3 4.4 r.y.⎕ref [r:numeric] (r.y.⎕ref).⎕ds ⍝ Use R to format the R array [1] 2.2 3.3 4.4 r.⎕eval 'mean(y)' 3.3 Complex, NA and NAN data typesThe APLX R interface defines three special object classes for NA ('Not Available'), NaN ('Not A Number') and complexnumber data, which R routines may return, or which you may want to pass as arguments into R functions. For example, the following R expression returns a complex number: c←r.⎕eval '3+4i' c [r:complex] c.format 3+4i Instances of these object classes can be created by using NA←'r' ⎕new 'NA' NA [r:NA] NAN←'r' ⎕new 'NAN' r.z←55.6 77.4 NAN 81 NA r.z 55.6 77.4 [r:NAN] 81 [r:NA] r.sqrt (⊂r.z) 7.456540753 8.797726979 [r:NAN] 9 [r:NA] The complex class allows you to create either a single complex number, by using a constructor with two numbers for real/imaginary parts: c←'r' ⎕new 'complex' 2 3 c [r:complex] c.format 2+3i or to build an R complex array by passing an array of length2 vectors of the real and imaginary parts of each complex number: m←'r' ⎕new 'complex' (3 2⍴(1 2) (3 4) (5 6) (7 8) (9 10) (11 12)) m [r:matrix] m.format 1+ 2i 3+ 4i 5+ 6i 7+ 8i 9+10i 11+12i You can access or specify the real and imaginary parts directly using the pseudoproperties real and imag of the complex object: m.real 1 3 5 7 9 11 m.imag←3 2⍴.1×⍳6 m.format 1+0.1i 3+0.2i 5+0.3i 7+0.4i 9+0.5i 11+0.6i m.imag 0.1 0.2 0.3 0.4 0.5 0.6 NAs and NaNs are also supported in Complex arrays: v←'r' ⎕new 'complex' ((3.2 3.4) NA (1.1 8.2)) v.format 3.2+3.4i NA 1.1+8.2i v.real 3.2 [r:NA] 1.1 v.imag 3.4 [r:NA] 8.2 (r.sqrt v).format 1.983563+0.857043i NA 2.164885+1.893865i Advanced R data typesOther R types, such as factors and lists, are left 'boxed up' as references to the underlying R object (unless you use lst←r.⎕eval 'list(name="Fred",age=99) lst [r:list] lst.⎕val Fred 99 ⎕display lst.⎕val An object which is still boxed up can be passed as an argument to an R function: r.length lst 2 r.names lst name age As a convenience you can also write this last example as: lst.length 2 lst.names name age This works because APLX treats the expression obj.function arg1,arg2,... ...as equivalent to: r.function obj,arg1,arg2,... Examining an object with



Copyright © 19962010 MicroAPL Ltd