Interfaces/R/Rserve/Formats

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

The result of an Rserve get command may be a SEXP expression - a recursively encoded list of names and values. Such expressions can be converted to a map (a two column table of names and values), which is a more convenient form for J. The core methods are:

    • rdgetexp or Rgetexp returns the original SEXP
    • rdget or Rget returns a map

In each case, tag labels in attributes have names preceded with a ` (backtick):

Rgetexp

Rgetexp returns the result in the original SEXP format. This is useful for debugging, but inconvenient for practical use.

   Rgetexp 'x=factor(c("one","two","three","four"))'
┌────────────────────────────────────────────┬───────┐
│┌────────────────────┬───────┬──────┬──────┐│2 4 3 1│
││┌────┬───┬─────┬───┐│`levels│factor│`class││       │
│││four│one│three│two││       │      │      ││       │
││└────┴───┴─────┴───┘│       │      │      ││       │
│└────────────────────┴───────┴──────┴──────┘│       │
└────────────────────────────────────────────┴───────┘

Rget

Rget returns the result as a map, i.e. a 2-column matrix of names and values:

   Rget 'x'
┌───────┬────────────────────┐
│`class │factor              │
├───────┼────────────────────┤
│`data  │2 4 3 1             │
├───────┼────────────────────┤
│`levels│┌────┬───┬─────┬───┐│
│       ││four│one│three│two││
│       │└────┴───┴─────┴───┘│
└───────┴────────────────────┘

The tag `data' is used where the name is not given.

Where the SEXP is nested, the name paths are separated by $:

   Rcmd 'data(OrchardSprays)'
   Rget 'OrchardSprays'
┌─────────────────┬─────────────────────────────────────────────────────────────...
│`class           │data.frame                                                   ...
├─────────────────┼─────────────────────────────────────────────────────────────...
│`row.names       │1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 2...
├─────────────────┼─────────────────────────────────────────────────────────────...
│colpos           │1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4...
├─────────────────┼─────────────────────────────────────────────────────────────...
│decrease         │57 95 8 69 92 90 15 2 84 6 127 36 51 2 69 71 87 72 5 39 22 16...
├─────────────────┼─────────────────────────────────────────────────────────────...
│rowpos           │1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7...
├─────────────────┼─────────────────────────────────────────────────────────────...
│treatment$`class │factor                                                       ...
├─────────────────┼─────────────────────────────────────────────────────────────...
│treatment$`data  │4 5 2 8 7 6 3 1 3 2 8 4 5 1 6 7 6 8 1 5 4 3 7 2 8 1 5 3 6 7 2...
├─────────────────┼─────────────────────────────────────────────────────────────...
│treatment$`levels│┌─┬─┬─┬─┬─┬─┬─┬─┐                                            ...
│                 ││A│B│C│D│E│F│G│H│                                            ...
│                 │└─┴─┴─┴─┴─┴─┴─┴─┘                                            ...
└─────────────────┴─────────────────────────────────────────────────────────────...

Rget can also take an optional left argument that specifies the portion of the map that should be returned.

   'decrease' Rget 'OrchardSprays'
57 95 8 69 92 90 15 2 84 6 127 36 51 2 69 71 87 72 5 39 22 16 72 4 130 4 114 9  ...
   'treatment' Rget 'OrchardSprays'
┌───────┬───────────────────────────────────────────────────────────────────────...
│`class │factor                                                                 ...
├───────┼───────────────────────────────────────────────────────────────────────...
│`data  │4 5 2 8 7 6 3 1 3 2 8 4 5 1 6 7 6 8 1 5 4 3 7 2 8 1 5 3 6 7 2 4 5 4 7 1...
├───────┼───────────────────────────────────────────────────────────────────────...
│`levels│┌─┬─┬─┬─┬─┬─┬─┬─┐                                                      ...
│       ││A│B│C│D│E│F│G│H│                                                      ...
│       │└─┴─┴─┴─┴─┴─┴─┴─┘                                                      ...
└───────┴───────────────────────────────────────────────────────────────────────...

Extracting Results

The utilities defined in the rbase script can retrieve both individual results, as well as an "object" and all its decendants from the map structure, for example:

   '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
   lxy=. Rget 'lm(y ~ x)'
   Rmap lxy   NB. monadic Rmap returns keys from map structure
┌──────┬──────┬────┬────────────────────────┬──────────────┬───────────┬────────┬...
│`class│assign│call│coefficients$(Intercept)│coefficients$x│df.residual│effects$│...
└──────┴──────┴────┴────────────────────────┴──────────────┴───────────┴────────┴...
   'coefficients$x' Rmap lxy
0.10285714286
   'coefficients' Rmap lxy
┌───────────┬─────────────┐
│(Intercept)│3.9942857143 │
├───────────┼─────────────┤
│x          │0.10285714286│
└───────────┴─────────────┘
   'qr$qr$`data' Rmap lxy
_2.64575  _79.3725
0.377964    31.749
0.377964 0.0334734
0.377964 _0.155509
0.377964 _0.344491
0.377964 _0.533473
0.377964 _0.722456