bm_expr: Mapper for general expressions

View source: R/mapper_expr.R

bm_exprR Documentation

Mapper for general expressions

Description

Constructs an expression mapping

Usage

bm_expr(
  expr,
  assume = character(0),
  labels = NULL,
  .envir = rlang::caller_env()
)

Arguments

expr

An expression object, typically created using rlang::quo(), that can be evaluated in a data mask containing the root variables, derived variables, and data variables.

assume

character vector listing valid assumptions for the combination of the expression and data, derived variables, and root variables. This can be used to specify assumptions that allow for more efficient Jacobian calculations. For example, if the expression is row-wise in the data and derived variables, then assume = "rowwise" can be used to indicate that the Jacobian with respect to derived variables can be calculated simultaneously for all rows. Supported assumptions are "rowwise", "linear", "additive", and "no_root".

labels

list of root, derived, and suffix. The root and derived elements specify the pronoun labels for the data mask for root and derived variables. Default "root" and "derived", respectively. For example, if root = "latent", then the root variables will be accessible in the expression as .latent$varname. If suffix is non-NULL, a character string specifying a suffix to add to the root variable names when making them directly available to the expression. Default is NULL, equivalent to "". For example, if suffix = "_latent", then a root variable named "x" will be available as "x_latent" in the expression, in addition to the data mask pronoun version.

.envir

The environment for the expression evaluation. By default, this is set to the caller environment.

Details

The input is currently ignored.

data should be a list with data objects, with the main object called data. If is_rowwise == TRUE, the number of rows in the data data.frame determines the number of rows in the output, and the columns can be used as constants in the expression, accessed via .data$colname, .data.$colname, or .data.[["colname"]].

Value

A bm_expr mapper object

See Also

bru_mapper, bru_mapper_generics

Other mappers: bm_aggregate(), bm_collect(), bm_const(), bm_factor(), bm_fm_mesh_1d, bm_fmesher(), bm_harmonics(), bm_index(), bm_linear(), bm_logitaverage(), bm_logsumexp(), bm_marginal(), bm_matrix(), bm_multi(), bm_pipe(), bm_reparam(), bm_repeat(), bm_scale(), bm_shift(), bm_sum(), bm_taylor(), bru_get_mapper(), bru_mapper()

Examples

# Basic expression with only root variables ("x").
(m <- bm_expr(rlang::quo(cos(x))))
ibm_eval(m, list(), list(x = 1:5))
ibm_eval2(m, list(), list(x = 1:5))

# Expression with data
(m <- bm_expr(rlang::quo(cos(x) * .data$z)))
the_data <- list(data = data.frame(z = 11:15))
ibm_eval(m, list(), list(x = 1:5), data = the_data)
ibm_eval2(m, list(), list(x = 1:5), data = the_data)

# Expression with data, root variables, and derived variables.
(m <- bm_expr(
  rlang::quo(sin(x_latent) + cos(.effect$y) * .data$z),
  labels = list(root = "latent", derived = "effect", suffix = "_latent")
)
)
ibm_eval(
  m,
  list(),
  derived = list(y = 2:6), # y = x + 1
  jacobians = list(y = list(x = Matrix::Diagonal(1.0, 5))),
  data = the_data,
  state = list(x = 1:5)
)


inlabru documentation built on July 28, 2026, 9:07 a.m.