This function replicates pattern objects, replacing placeholder keywords in each iteration with values from the provided data. This allows efficiently creating R Markdown documents with many repeating pieces which may shift alongside the underlying data.
heddle(data, pattern, ..., strip.whitespace = FALSE)
Input dataframe to pull replacement values from. Accepts either vector or dataframe inputs.
The base pattern, as either an atomic vector (which will be
recycled for every value in your data) or a vector of the same length as
your data (which will be applied element-wise to your data, so that
Values indicating what placeholders in the pattern should be replaced -- see "Specifying replacement values" below for more.
A boolean (TRUE/FALSE) value indicating if whitespace should be removed from the replacement variable. Toggle this on if you're using the variable in chunk labels or similar places.
Returns a character vector of the pattern with placeholders replaced by variables.
heddle can accept multiple different values for
on how you call it.
data is a vector (which is the case when either
heddle on a vector directly, or using it in a
... should be unnamed strings
matching the values to be replaced. If any argument passed to
isn't found in the pattern, a warning will be raised -- use
replicate patterns without replacing any values.
data is a dataframe (which is the case both when calling
heddle on a dataframe directly or using it in combination with
... should be a set of name = variable
pairs, with the name matching the keyword to be replaced by that variable.
Names should be quoted, variable names don't need to be. As with vectors,
if any argument passed to
... isn't found in the pattern, a warning
will be raised.
# When passed a vector, heddle replaces all placeholders passed to ... # with each value spList <- unique(iris$Species) heddle(spList, "SPECIES CODE GWAR ", "GWAR")#>  "SPECIES CODE setosa " "SPECIES CODE versicolor " #>  "SPECIES CODE virginica "heddle(spList, "SPECIES CODE GWAR ", "GWAR", "CODE")#>  "SPECIES setosa setosa " "SPECIES versicolor versicolor " #>  "SPECIES virginica virginica "heddle("test string", "pattern tk", "tk", strip.whitespace = TRUE)#> teststring #> "pattern teststring"# When passed a dataframe, heddle uses "Name" = Variable syntax to determine # which values should replace which placeholders spList <- data.frame(Species = c(unique(iris$Species), "test string")) heddle(spList, "SPECIES CODE GWAR ", "GWAR" = Species)#>  "SPECIES CODE 1 " "SPECIES CODE 2 " #>  "SPECIES CODE 3 " "SPECIES CODE test string "heddle(spList, "SPECIES CODE GWAR ", "GWAR" = Species, "CODE" = Species)#>  "SPECIES 1 1 " "SPECIES 2 2 " #>  "SPECIES 3 3 " "SPECIES test string test string "