absorb {cheese}R Documentation

Absorb values into a string containing keys

Description

Populate string templates containing keys with their values. The keys are interpreted as regular expressions. Results can optionally be evaluated as R expressions.

Usage

absorb(
    key, 
    value, 
    text, 
    sep = "_",
    trace = FALSE,
    evaluate = FALSE
)

Arguments

key

A vector that can be coerced to type character.

value

A vector with the same length as key.

text

A (optionally named) character vector containing patterns.

sep

Delimiter to separate values by in the placeholder for duplicate patterns. Defaults to "_"

trace

Should the recursion results be printed to the console each iteration? Defaults to FALSE.

evaluate

Should the result(s) be evaluated as R expressions? Defaults to FALSE.

Details

The inputs are iterated in sequential order to replace each pattern with its corresponding value. It is possible that a subsequent pattern could match with a prior result, and hence be replaced more than once. If duplicate keys exist, the placeholder will be filled with a collapsed string of all the values for that key.

Value

Author(s)

Alex Zajichek

Examples

#Simple example
absorb(
    key = c("mean", "sd", "var"),
    value = c("10", "2", "4"),
    text = 
        c("MEAN: mean, SD: sd",
          "VAR: var = sd^2",
          MEAN = "mean"
        )
)

#Evaluating results
absorb(
    key = c("mean", "mean", "sd", "var"),
    value = c("10", "20", "2", "4"),
    text = c("(mean)/2", "sd^2"),
    sep = "+",
    trace = TRUE,
    evaluate = TRUE
) %>%
    rlang::flatten_dbl()


[Package cheese version 0.1.2 Index]