Interfaces/R/Rserve/Examples

From J Wiki
< Interfaces‎ | R‎ | Rserve
Jump to navigation Jump to search
Rserve | Installation | Methods | Formats | Examples | Quirks | Extensions


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'
┌┬──┬┬──┬─────────────────┐
││`x││`y│┌─┬─────────────┐│
││  ││  ││{│┌─┬─┬───────┐││
││  ││  ││ ││+│x│┌─┬─┬─┐│││
││  ││  ││ ││ │ ││*│2│y││││
││  ││  ││ ││ │ │└─┴─┴─┘│││
││  ││  ││ │└─┴─┴───────┘││
││  ││  │└─┴─────────────┘│
└┴──┴┴──┴─────────────────┘

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

R Session output

You can capture the output of an R command to the R session as follows:

   Rcmd 'library(MASS)'
   Rcmd 'res <- lm(data=iris, Sepal.Width ~ Petal.Width)'
   LF joinstring Rget 'capture.output(summary(res))'     NB. require 'strings' needed for joinstring pre-J7

Call:
lm(formula = Sepal.Width ~ Petal.Width, data = iris)

Residuals:
     Min       1Q   Median       3Q      Max
-1.09907 -0.23626 -0.01064  0.23345  1.17532

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  3.30843    0.06210  53.278  < 2e-16 ***
Petal.Width -0.20936    0.04374  -4.786 4.07e-06 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.407 on 148 degrees of freedom
Multiple R-squared:  0.134,	Adjusted R-squared:  0.1282
F-statistic: 22.91 on 1 and 148 DF,  p-value: 4.073e-06