Description Arguments Value Declaration See Also Examples
TYPEOF
is a function-like macro that determines the type of an
SEXPREC
object.
x |
an |
A SEXPTYPE
value.
#define TYPEOF(x) ((x)->sxpinfo.type)
In Rinternals.h.
typeof
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | # Determine R objects' SEXPTYPEs
sexptype <- inline::cfunction(c(x = "ANY"),
' SEXP type_value;
type_value = PROTECT(Rf_ScalarInteger(TYPEOF(x)));
UNPROTECT(1);
return type_value;
')
# NULL, returns 0
sexptype(NULL)
# an empty pairlist also returns 0
sexptype(pairlist())
# symbol, returns 1
sexptype(as.symbol('a'))
# dotted pair list, returns 2
sexptype(pairlist(a = 1))
# closure, returns 3
sexptype(function(x) {x + 1})
# environment, returns 4
sexptype(.GlobalEnv)
# promises, returns 5
# But how to get one?
# language (call and formulae), returns 6
sexptype(call("sin", pi))
sexptype(~ x + 2)
# special forms, returns 7
sexptype(.Internal)
# builtin non-special forms, returns 8
sexptype(.Primitive("sqrt"))
# "scalar" string type (internal only), would return 9
# No way of getting hold of one of this in R land
# logical vectors, returns 10
sexptype(TRUE)
sexptype(FALSE)
sexptype(NA)
# codes 12 and 13 are no longer in use
# used to be for factors and ordered factors in the 1990s
# integer vectors, returns 13
sexptype(1L)
sexptype(factor('as')) # still an integer vector
# real vectors, returns 14
sexptype(1)
sexptype(pi)
sexptype(double(1))
sexptype(numeric(1))
# complex vectors, returns 15
sexptype(1i)
sexptype(0i)
sexptype(0 + 1i)
sexptype(NA_complex_)
# string vectors , returns 16
sexptype("alice in wonderland")
# TODO: dot-dot-dot object, returns 17
# TODO: "any" args, returns 18
# generic vectors, i.e., lists, returns 19
sexptype(list())
# expression, returns 20
sexptype(expression(x + 2))
# TODO: byte code, returns 21
# TODO: external pointer, returns 22
# TODO: weak reference, returns 23
# raw bytes, returns 24
sexptype(raw(2))
# TODO: S4, non-vector, returns 25
# TODO: fresh node created in new page, returns 30
# TODO: node released by GC, returns 31
# TODO: Closure or Builtin or Special, returns 99
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.