Description Usage Arguments Details Value Note Author(s) See Also Examples
Interpolate evaluated expressions into strings.
1 2 3 4 5 6 7 | interpolate(str, begin = ".(", end = ")", envir = arg_env(str,
environment()))
interply(str, begin = ".(", end = ")", envir = arg_env(str,
environment()))
str %#% args
|
str |
A template string. For |
begin |
The beginning delimiter. |
end |
The ending delimiter. |
envir |
The environment evaluation takes place in. Defaults to the lexical environment of the template. |
args |
A list or named vector; interpolation happens in an environment with these values bound. |
For interpolate, the argument is scanned for substrings that
look like ".(expr)". These are replaced by the evaluation of
the expression. Expressions will be matched respecting the
balancing of braces and quotes.
interply generates a function that performs this
interpolation for a particular string. The interpolation is
repeated along the arguments applied to the function (so the usage
is interply(".(x),.(y)")(x=val, y=val), similar to
mply and qqply.) Note unnamed arguments
can be referred to as ..1, ..2, etc.
%#% is a shortcut for applying interpolation to data
from a list or named vector. string %#% values is equivalent to
interply(string) %()% values.
A character vector.
If accepting a formatting string from user data, it is prudent
to specify emptyenv() or a descendant of emptyenv()
populated only with "allowed" functions and data.
Peter Meilstrup
qq qqply mply
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | foo<-1; bar<-"two"; baz <- "III"
interpolate(c(".(foo),", "a .(bar)", "a .(foo) .(bar) .(baz)"))
interply("hello .(..1)")(c("world", "nurse", "kitty"))
interply("hello {{q}}", begin="{{", end="}}")(q=c("there", "you"))
"hello .(x)" %#% c(x="world")
#shell-style interpolation -- "$" start and no end delim
interply("$hello, ${paste(rep('hello', 3), collapse=' ')}, $'hi'",
begin="$", end=""
)(hello="hola")
# Compliant 99 Bottles implementation:
bottles <- interply(
".(ifelse(n%%100>0, n%%100, 'no more')) bottle.('s'[n%%100!=1]) of beer")
initcap <- function(x) {substr(x, 1, 1) <- toupper(substr(x, 1, 1)); x}
verse <- interply(
paste0(".(initcap(bottles(n=n))) on the wall, .(bottles(n=n)).\n",
".(c('Go to the store and buy some more,',",
" 'Take one down and pass it around,')[(n%%100!=0)+1])",
" .(initcap(bottles(n=n-1))) on the wall."))
cat(verse(n=99:0), sep="\n\n")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.