Skip to contents

Returns one cell of a spicy table as a character scalar, formatted exactly as the table displays it – same decimals, same p style, same interval punctuation, same journal style. Designed for inline R chunks in Quarto / R Markdown:

Smokers had higher odds (`r inline(tbl, smoking, "Yes", "or")`).

Usage

inline(x, variable, level = NULL, column = NULL, model = NULL)

Arguments

x

A table returned by table_regression(), table_categorical(), table_continuous(), table_continuous_lm(), table_outcome(), table_categorical_svy(), table_continuous_svy(), table_multiresponse() or table_battery() (default output) – every family as_structured() accepts. A stacked table of table_stack() holds several tables and is refused: pass the panel that holds the cell, attr(x, "panels")[["Women"]].

variable

The source variable, unquoted or as a string; or a fit-statistic token ("nobs", "r2", ...) or label ("n").

level

For a factor variable, the level, as a string: a category shown as a row. "(Missing)" addresses the missing-value category by role. A group of by is selected with model. In a regression table, the outcome category of a multinomial row or the equation of a Heckman row, under the header of its term, when the model sits beside others shown in rows.

column

A column token, or a {token} pattern. NULL (the default) returns the estimate-like column of the row when it is unambiguous: the family's primary estimate. That is the coefficient for table_regression() – always token "b": an exponentiated table changes its header to OR, IRR or HR, never its token – the contrast ("delta") or, across a numeric by, the slope ("b") for table_continuous_lm(), the mean ("m") or, on a median-only table, the median ("med", "med_iqr") for table_continuous(), and the count ("n") for table_categorical() and table_multiresponse(). A row carrying none of them refuses and lists its tokens. A table_continuous_lm() row that shows the mean of each group and no contrast (a by of three or more levels, or contrast = "none") has no default: it refuses and gives the address of a mean.

model

A group of by, or in a multi-model table the model: its label (as displayed) or its position. A value that is no displayed label is also looked up among the identities of the models ("M1", and the equations "outcome" and "selection" of a selection model alone, in any case; beside another class, the identifier of the model and the equation, "Heckman: outcome"), so a call survives the language of the table. See Groups and models.

Value

A character scalar.

Addressing

The row is found by identity, not by display text: variable names the source column (.variable in the typed body), level the level (.level). Custom labels, a style, or a translated display never change the call. As a convenience, a variable that matches no source column is looked up among the displayed labels before erroring. The missing-value category is addressed by level = "(Missing)" whatever its displayed (possibly translated or relabeled) label, through its row role, and the Total row of table_categorical() under total_row = TRUE by level = "Total", the same way. Fit statistics are addressed by their token as variable (inline(tbl, "nobs"), inline(tbl, "r2")) or by their label (inline(tbl, "n")). The token is the one show_fit_stats takes ("r2", "adj_r2", "aic", ...), the key of the row in the variable column of output = "data.frame". glance() and the long output name the same statistic as broom does (r.squared, adj.r.squared), a name inline() does not read. Identity comes first: a source column wins over a token or a label of the same name, and an exact label over a fit statistic's label read regardless of case. A variable that still matches the rows of two variables or statistics is refused, and the error names an address for each.

A statistic that belongs to a whole variable rather than to one of its levels sits on the variable's own row: the p of a table_categorical() block, its association measure, its SMD. Leave level out to cite it (inline(tbl, smoking, column = "p")). The same call cites the p of a by-group table_continuous() or table_continuous_svy(), which sits on the first group's row: when one row of the variable alone carries the statistic, that row is the address.

Groups and models

model selects a group of by in every table, by its label or its position, wherever the table sets the groups out: the column spanners of table_categorical(), table_categorical_svy() and table_multiresponse(), the margin included (inline(tbl, smoking, "Yes", "pct", model = "Female")), the per-group columns of table_continuous_lm(), headed Female and Male under the mean symbol M (inline(tbl, bmi, column = "emmean", model = "Female")), and the rows of table_continuous() and table_continuous_svy(), one per group (inline(tbl, bmi, model = "Female")). The group of missing by values is model = "(Missing)". In a multi-model table, model selects the model the same way. The cells that belong to no group (a p, the contrast of table_continuous_lm() and its interval, an association measure) are cited without model. In table_continuous() and table_continuous_svy(), the test, its p, the effect size and the SMD are statistics of the variable, printed once on the row of the first group: inline(tbl, bmi, column = "p"). Asked with model, whichever group it names, they are an error (class spicy_invalid_input) that gives that call.

level names a category shown as a row: the level of a variable, its row of missing values. In a regression table, it also names the rows under the header of a term: the outcome categories of a multinomial model beside other models (inline(tbl, age, "Student", model = "Adjusted")) and the two equations of a Heckman selection model beside other selection models ("selection", "outcome"). Alone, a multinomial or selection model has its categories or equations as column groups, which model selects by their label or position (inline(tbl, educ, model = "Outcome")): there, level = "outcome" is an error that gives that call. Beside a model of another class, a selection model has two groups as well, selected by their label (model = "Heckman: Selection") or position. Until spicy 0.14.0 it also selected the groups of table_continuous(), table_continuous_svy() and table_continuous_lm(): that use is an error (class spicy_defunct) whose message gives the call to use instead.

table_multiresponse() has one row per item, addressed by its column name, then the rows of none_row, addressed as "none" and "no_data", and the base row, the number of respondents, addressed as the fit statistic "nobs": inline(tbl, "nobs", model = "Total"). The item p and statistic belong to no group and are cited without model (inline(tbl, info_internet, column = "p")).

table_battery() has one row per item and one column per level of its scale: level names the column, as a level of the scale, the header of the summary column of top ("Agree + Strongly agree"), or "(Missing)", and model the group under by (inline(tbl, q1, "Agree", "pct", model = "Female")). The base n and the item p are cited without level.

Columns

The column is a token of the typed contract ("b", "se", "p", "ci", "or", "ame", "n", "pct", "m", ... – see as_structured()'s col_meta), never a display header. "ci" composes the interval with the style's brackets and separator, and so does every other interval token the table carries ("med_ci", "ame_ci", "assoc_ci"): each addresses its own bounds. A statistic the table prints with its degrees of freedom has a token for each of them: "<token>_df" for one ("partial_chi2_df"), "<token>_df_num" and "<token>_df_den" for the numerator and the denominator of an F. They are formatted as the table prints them: inline(tbl, sex, column = "F({partial_f_df_num}, {partial_f_df_den}) = {partial_f}") quotes the whole test.

Patterns

A column containing { is a pattern: each {token} is replaced by the corresponding cell, so one call quotes a full sentence fragment:

inline(tbl, smoking, "Yes", "{or} ({ci_label} {ci}; p = {p})")

{ci_label} inserts the interval label of the interval the pattern cites (95% CI, or Mdn 95% CI in a pattern quoting {med_ci}) – the first one when it cites several, the table's first when it cites none. Note that {p} carries the floor operator when the table does (<.001), so write p {p} rather than p = {p} in patterns that may hit the floor.

Errors

Every misaddressing is a classed error that lists the available choices: unknown variables list the variables, missing levels list the levels, unknown tokens list the table's tokens, ambiguous models and groups list them. A level that names a group of a by table points to model. A cell the table itself displays as undefined (an aliased coefficient's en-dash) refuses with the reason rather than pasting a dash into a sentence.

See also

as_structured() for the typed contract behind the addressing.

Examples

fit <- lm(wellbeing_score ~ age + sex, data = sochealth)
tbl <- table_regression(fit)
inline(tbl, age, column = "b")
#> [1] "0.04"
inline(tbl, sex, "Male", "{b} ({ci_label} {ci}; p {p})")
#> [1] "3.90 (95% CI [2.14, 5.65]; p <.001)"
inline(tbl, "n")
#> [1] "1200"

# A group of `by` is a `model`, whether the table sets it out as
# columns or as rows.
ct <- table_categorical(sochealth, select = smoking, by = sex)
inline(ct, smoking, "Yes", "pct", model = "Female")
#> [1] "21.6"
cn <- table_continuous(sochealth, select = bmi, by = sex)
inline(cn, bmi, model = "Female")
#> [1] "25.69"
inline(cn, bmi, column = "p")
#> [1] ".018"

# A battery: the level of the scale is `level`, the group `model`.
sat <- sochealth["sex"]
sat$health <- factor(sochealth$life_sat_health, levels = 1:5)
sat$work <- factor(sochealth$life_sat_work, levels = 1:5)
bt <- table_battery(sat, select = c(health, work), by = sex)
inline(bt, health, "5", "pct", model = "Female")
#> [1] "27.8"
inline(bt, health, column = "p")
#> [1] ".767"