Skip to contents

Class predicates returning a single logical indicating whether x inherits from the corresponding tabular S7 class. Use them to gate user-side code that branches on what a verb has returned, to write defensive helpers that wrap tabular pipelines, or to assert intermediate shapes during pipeline debugging.

Usage

is_tabular_spec(x)

is_tabular_grid(x)

is_col_spec(x)

is_header_node(x)

is_sort_spec(x)

is_style_node(x)

is_style_layer(x)

is_style_spec(x)

is_pagination_spec(x)

is_preset_spec(x)

is_subgroup_spec(x)

is_inline_ast(x)

Arguments

x

Object to test. Any R value. Each predicate returns TRUE if x inherits from the named class, FALSE otherwise.

Value

A single TRUE / FALSE. Use in if / stopifnot guards, or chain into validation helpers.

A length-1 logicalTRUE or FALSE. Never NA.

Details

Twelve predicates cover the full S7 surface:

predicatetests forproduced by
is_tabular_spec()tabular_spectabular() and every build verb
is_tabular_grid()tabular_gridas_grid()
is_col_spec()col_speccol_spec()
is_header_node()header_nodeheaders() (internal nodes)
is_sort_spec()sort_specsort_rows()
is_style_node()style_nodestyle() (per-cell style)
is_style_layer()style_layerstyle() (one per call)
is_style_spec()style_specstyle() (the cascade root)
is_pagination_spec()pagination_specpaginate()
is_preset_spec()preset_specpreset(), set_preset()
is_subgroup_spec()subgroup_specsubgroup()
is_inline_ast()inline_astparse_inline() (post-format)

Predicates never error — they return FALSE for NULL, vectors, objects of any other class, and S7 objects from other packages. Use them at any layer of a user's pipeline without a defensive tryCatch().

See also

Class definitions: tabular_classes.

Verbs producing each class: tabular(), col_spec(), headers(), sort_rows(), style(), paginate(), preset(), as_grid().

Examples

# ---- Example 1: Gate user-side code on the spec class ----
#
# A user-side helper that pre-validates its input before piping
# into a downstream tabular chain. The predicate returns FALSE
# for any non-spec input without raising, so the helper can emit
# a friendlier error than tabular's own S7 validator would.
add_safety_footnote <- function(spec) {
  if (!is_tabular_spec(spec)) {
    stop("`spec` must be a tabular_spec; build one with tabular().")
  }
  spec
}

demo <- tabular(cdisc_saf_demo, titles = "Demographics")
is_tabular_spec(demo)         # TRUE
#> [1] TRUE
is_tabular_spec("not a spec") # FALSE — does not raise
#> [1] FALSE
add_safety_footnote(demo)

 

Demographics

 

variablestat_labelplacebodrug_50drug_100Total
Age (years)n869672254
Age (years)Mean (SD)75.2 (8.59)76.0 (8.11)73.8 (7.94)75.1 (8.25)
Age (years)Median76.078.075.577.0
Age (years)Q1, Q369.2, 81.871.0, 82.070.5, 79.070.0, 81.0
Age (years)Min, Max52, 8951, 8856, 8851, 89
Sex, n (%)F53 (61.6)55 (57.3)35 (48.6)143 (56.3)
Sex, n (%)M33 (38.4)41 (42.7)37 (51.4)111 (43.7)
Race, n (%)WHITE78 (90.7)90 (93.8)62 (86.1)230 (90.6)
Race, n (%)BLACK OR AFRICAN AMERICAN8 (9.3)6 (6.2)9 (12.5)23 (9.1)
Race, n (%)ASIAN0 (0.0)0 (0.0)0 (0.0)0 (0.0)
Race, n (%)AMERICAN INDIAN OR ALASKA NATIVE0 (0.0)0 (0.0)1 (1.4)1 (0.4)
# ---- Example 2: Assert intermediate shapes during debugging ---- # # When chaining many verbs, dropping `stopifnot()` between verbs # gives a clear stack trace if a verb silently returns the wrong # type. Predicates are cheap (single S7 dispatch each) and never # error, so they are safe to leave in pipelines during dev. n <- stats::setNames(cdisc_saf_n$n, cdisc_saf_n$arm_short) spec <- tabular( cdisc_saf_demo, titles = c("Table 14.1.1", "Demographics", "Safety Population") ) |> cols( variable = col_spec(usage = "group", label = "Characteristic"), stat_label = col_spec(label = "Statistic"), placebo = col_spec(label = "Placebo\nN={n['placebo']}", align = "decimal"), drug_50 = col_spec(label = "Drug 50\nN={n['drug_50']}", align = "decimal"), drug_100 = col_spec(label = "Drug 100\nN={n['drug_100']}", align = "decimal"), Total = col_spec(label = "Total\nN={n['Total']}", align = "decimal") ) |> sort_rows(by = c("variable", "stat_label")) stopifnot( is_tabular_spec(spec), is_col_spec(spec@cols[["placebo"]]), is_sort_spec(spec@sort) ) grid <- as_grid(spec) stopifnot(is_tabular_grid(grid))