Contents
The following examples use the interactive methods defined in z, each with an "R" prefix. These are simply cover verbs for corresponding methods in the rserve locale.
Connecting
First ensure that the Rserve server is already started. If so, any use of an Rserve method will automatically connect to the R server, for example:
Rget 'pi' 3.14159
To explicitly open the connection, use verb Ropen, which takes optional arguments of host and port number (defaults, localhost and 6631):
Ropen'' NB. default Ropen 'localhost';4418 NB. use port 4418
Array Shape
The R shape order is preserved (arrays are in column-major order):
Rget 'matrix(1:6,2,3)' 1 3 5 2 4 6 $ Rget 'array(1:6,c(2,3,4,5))' 5 4 2 3
The following expression flips J arrays into R order:
rflip=: _2 |: |.@$ $ , (rflip 1 + i.2 3 4) -: Rget 'array(1:24,c(2,3,4))' 1
Attribute Lists
R data can have attributes. The J/R interface returns these in one of two formats.
For example:
Rcmd 'x=factor(c("one","two","three","four"))'
Rget 'x' NB. return map
┌───────┬────────────────────┐
│`class │factor │
├───────┼────────────────────┤
│`data │2 4 3 1 │
├───────┼────────────────────┤
│`levels│┌────┬───┬─────┬───┐│
│ ││four│one│three│two││
│ │└────┴───┴─────┴───┘│
└───────┴────────────────────┘
Rgetexp 'x' NB. return SEXP
┌────────────────────────────────────────────┬───────┐
│┌────────────────────┬───────┬──────┬──────┐│2 4 3 1│
││┌────┬───┬─────┬───┐│`levels│factor│`class││ │
│││four│one│three│two││ │ │ ││ │
││└────┴───┴─────┴───┘│ │ │ ││ │
│└────────────────────┴───────┴──────┴──────┘│ │
└────────────────────────────────────────────┴───────┘
Booleans
R has TRUE=1, FALSE=0, and NA=2:
Rget 'c(TRUE,FALSE,NA,TRUE,TRUE,FALSE)' 1 0 2 1 1 0
Command Results
Rcmd evaluates the command only, while Rget evaluates a command and also returns the result.
Rcmd 'x=array(1:8,c(2,4))' NB. cmd = evaluate only Rget 'x*10' NB. get = evaluate and return 10 30 50 70 20 40 60 80
Function Definition
Function definition is by assignment, as in J:
Rcmd 'foo <- function(x,y) {x + 2 * y}'
Rget 'typeof(foo)'
closure
Rget 'foo(5,3)'
11
Rgetexp 'foo'
┌─────────────────────────────────────┬───────────────────────────┐
│┌───────────────────────────┬───────┐│┌┬──┬┬──┬─────────────────┐│
││┌─────────────────────────┐│`source││││`x││`y│┌─┬─────────────┐││
│││function(x,y) {x + 2 * y}││ ││││ ││ ││{│┌─┬─┬───────┐│││
││└─────────────────────────┘│ ││││ ││ ││ ││+│x│┌─┬─┬─┐││││
│└───────────────────────────┴───────┘│││ ││ ││ ││ │ ││*│2│y│││││
│ │││ ││ ││ ││ │ │└─┴─┴─┘││││
│ │││ ││ ││ │└─┴─┴───────┘│││
│ │││ ││ │└─┴─────────────┘││
│ │└┴──┴┴──┴─────────────────┘│
└─────────────────────────────────────┴───────────────────────────┘
In at least some cases, the Windows version of Rserve does not return the first box of the foo structure above.
NA Value
R has a NA value, that has no J equivalent. It is convenient to define a noun in J for this purpose. The default value used is __ (negative infinity):
Rget 'c(1,2,Inf,-Inf,NaN,NA)' 1 2 _ __ _. __
Scalars
R has no scalars, and treats single numbers and characters as 1-element lists:
Rcmd 'x = c("a","b","c","d","e","f","g","h")'
Rcmd 'dim(x) = c(2,4)'
Rget 'x' NB. note boxed result
┌─┬─┬─┬─┐
│a│c│e│g│
├─┼─┼─┼─┤
│b│d│f│h│
└─┴─┴─┴─┘
Rcmd 'x = c("abc","b","c","d","e","fore","g","h")'
Rcmd 'dim(x) = c(2,4)'
Rget 'x'
┌───┬─┬────┬─┐
│abc│c│e │g│
├───┼─┼────┼─┤
│b │d│fore│h│
└───┴─┴────┴─┘
Rset
Rset works with simple data values. It cannot be used to send attributes other than the shape, which is handled automatically by the interface.
Rget 'abc' [ 'abc' Rset 'qwerty' qwerty Rget 'abc' [ 'abc' Rset i.2 3 0 1 2 3 4 5 'x' Rset 12 18 24 30 36 42 48 'y' Rset 5.27 5.68 6.25 7.21 8.02 8.71 8.42 Rcmd 'lxy=lsfit(x,y)' Rget 'lxy$coefficients' ┌─────────┬────────┐ │Intercept│3.99429 │ ├─────────┼────────┤ │X │0.102857│ └─────────┴────────┘ Rget 'lxy$residuals' 0.0414286 _0.165714 _0.212857 0.13 0.322857 0.395714 _0.511429
