Interfaces/R/Rserver/Formats/SEXP

From J Wiki
Jump to navigation Jump to search
Rserver | Methods | Formats | Examples | Quirks

Calls to R return one of:

  • an integer
  • a character string
  • a SEXP expression

SEXP is the internal format used by R objects, and is used in most cases. It is defined in Rinternals.h.

A SEXP expression is either data, or an attribute;data pair:

  • data is a list without attributes.
  • attribute is a list of value;tag pairs, where the tag is an R internal name, such as class.

The data items and tag values may themselves be SEXP expressions.

In J, in order to distinguish an attribute;data pair from a data item that is a 2-element list, the attribute;data pair is given as a 1x2 matrix. This suffices, since any other data item must be a singleton or a list.

In J also, the tag names are prefixed with  (backtick).

Example 1

In this example, the data is the list 2 4 3 1. The attribute class has value factor, and the attribute levels has value ;:'four one three two'.

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

Example 2

An array is represented as a data list, with the dim attribute:

   Rgetexp 'array(1:6,c(2,3,4))'
┌────────────┬───────────────────────────────────────────────┐
│┌─────┬────┐│1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6│
││2 3 4│`dim││                                               │
│└─────┴────┘│                                               │
└────────────┴───────────────────────────────────────────────┘

Example 3

The names attribute has names for the data items:

   '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
   'att data'=. ,Rgetexp 'lsfit(x,y)'
   att
┌─────────────────────────────────────┬──────┐
│┌────────────┬─────────┬─────────┬──┐│`names│
││coefficients│residuals│intercept│qr││      │
│└────────────┴─────────┴─────────┴──┘│      │
└─────────────────────────────────────┴──────┘
   $data
4

The items in data may themselves be SEXP expressions:

   0 pick data
┌──────────────────────┬────────────────┐
│┌─────────────┬──────┐│3.99429 0.102857│
││┌─────────┬─┐│`names││                │
│││Intercept│X││      ││                │
││└─────────┴─┘│      ││                │
│└─────────────┴──────┘│                │
└──────────────────────┴────────────────┘

   1 pick data
0.0414286 _0.165714 _0.212857 0.13 0.322857 0.395714 _0.511429

   2 pick data
1

   3 pick data
┌───────────────────────────────────────────────┬───────────────────────────────────────────────────...
│┌────────────────────────────┬──────┬──┬──────┐│┌──────────────────────────────────────────────────...
││┌──┬──┬─────┬────┬─────┬───┐│`names│qr│`class│││_18.7319 3.26561 _0.219372 0.0961104 0.261593 0.30...
│││qt│qr│qraux│rank│pivot│tol││      │  │      │││                                                  ...
││└──┴──┴─────┴────┴─────┴───┘│      │  │      │││                                                  ...
│└────────────────────────────┴──────┴──┴──────┘││                                                  ...
│                                               ││                                                  ...
│                                               ││                                                  ...
│                                               ││                                                  ...
│                                               ││                                                  ...
│                                               ││                                                  ...
│                                               │└──────────────────────────────────────────────────...
└───────────────────────────────────────────────┴───────────────────────────────────────────────────...