Learn R Programming

cheese (version 0.0.3)

absorb: Absorb values of a key-value pair into an arbitrary character string containing keys

Description

Populates user-specified string templates containing keys as placeholders, with the values. The keys are interpreted as regular expressions. Results can optionally be evaluated as R expressions.

Usage

absorb(
    key, 
    value, 
    text, 
    sep = "|",
    print = FALSE,
    evaluate = FALSE
)

Arguments

key

Vector of keys that can be coerced to type character.

value

Vector of values with positions corresponding to the key.

text

Vector of character strings containing sequences of characters and keys/patterns where values should be filled.

sep

Character to separate values by in the placeholder in the event of duplicate keys (patterns). Defaults to "|"

print

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

evaluate

Should the resulting strings be evaluated as R expressions? Defaults to FALSE.

Value

If evaluate = FALSE (default), a character vector the same length as text with all matching patterns replaced by their value. If evaluate = TRUE, a list with the same length as text is returned with the result of the evaluation of each string.

Details

The algorithm iterates the provided value vector (in sequential order) and recursivley replaces substrings where there is a matching pattern of a key. Thus, it is possible that a subsequent key could match with a previous value, 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.

Examples

Run this code
# NOT RUN {
require(tidyverse)

#1) 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"
        )
)

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


# }

Run the code above in your browser using DataLab