Description Usage Arguments Details Examples
template.replace
replaces keys within special markups with
values in a so-called template file. Pieces of R code can be put into
the markups of the template file, and are evaluated during the
replacement.
1 2 |
text |
vector of character strings, the template text. |
replacement |
the list values to replace in |
eval |
boolean, |
key.pattern |
custom pattern for key replacement (see below) |
code.pattern |
custom pattern for code replacement (see below) |
In most cases, a computational code reads its inputs from a text file. A template file is like an input file, but where some missing values, identified with generic keys, will be replaced by specific values.
By default, the keys are enclosed into markups of the form $(KEY)
.
Code to be interpreted with R can be put in the template text. Pieces
of code must be enclosed into markups of the form
@{CODE}
. This is useful for example for formating the key
values (see example). For interpreting the code, set eval = TRUE
.
Users can define custom patterns. These patterns must be
perl-compatible regular expressions (see regexpr
.
The default ones are:
1 2 | key.pattern = "\\$\\(KEY\\)"
code.pattern = "@\\{CODE\\}"
|
Note that special characters have to be escaped both (one for perl, one for R).
1 2 3 4 5 6 7 8 9 | txt <- c("Hello $(name)!", "$(a) + $(b) = @{$(a)+$(b)}",
"pi = @{format(pi,digits=5)}")
replacement <- list(name = "world", a = 1, b = 2)
# 1. without code evaluation:
txt.rpl1 <- template.replace(txt, replacement)
print(txt.rpl1)
# 2. with code evalutation:
txt.rpl2 <- template.replace(txt, replacement, eval = TRUE)
print(txt.rpl2)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.