extract_values: Extract and Modify Values in an Expression

Description Usage Arguments Value Examples

View source: R/equation2latex.R

Description

This function extracts symbols from an expression and evaluates them in a manner the user specifies. First, each symbol is evaluated in the supplied envir. If it is defined in that environment and is not bound to a function, extract_values then applies pred to the value. If pred returns TRUE, the value is then transformed by fun.

Usage

1
extract_values(exp, env, pred = function(val) TRUE, fun = identity, ...)

Arguments

exp

A quoted expression.

env

An environment in which to evaluate values in the expression. Defaults to the caller environment.

pred

A predicate function that returns a length-one logical when applied to evaluated values within the expression. Defaults to TRUE.

fun

An optional function to apply to each evaluated value. extract_values will return whatever this function returns for each value successfully evaluated in env. Defaults to identity - which leaves the value unchanged.

...

Additional arguments to fun.

Value

A named vector or list of the successfully evaluated values. If none were successfully evaluated, an empty list.

Examples

1
2
3
4
5
6
 envir <- new.env()
 envir$A <- square(1:9)
 B <- square(4, 5, 6, 7)
 k <- 2
 extract_values(quote(A + B), env = envir, pred = function(x) nrow(x) >2, fun = mean)
 extract_values(quote(k * A), pred = is.matrix, fun = function(x) x %^% 3)

ryan-heslin/matador documentation built on Dec. 22, 2021, 8:17 p.m.