pp: Ruby-like string interpolation

Description Usage Arguments Examples

View source: R/pp.r

Description

To interpolate any R object into a string, it can be wrapped in #{...} and within a string. This is equivalent to evaluating whatever is in this block within the current environment (see base::environment when called with no arguments).

Usage

1
pp(..., envir = parent.frame(), sep = "", collapse = "")

Arguments

...

arbitrary many character vectors, that will be concatenated using the sep parameter as delimiter. Any non-length 1 vectors will also be collapsed using the collapse parameter.

envir

environment. Where to evaluate any interpolated strings, by default parent.frame(), the current local environment.

sep

character. The delimiter to use to concatenate passed strings, by default the empty string ''.

collapse

character. The delimiter to use to concatenate passed character vectors of length greater than 1, by default the empty string ''.

Examples

1
2
3
4
5
6
7
x <- 1; y <- 2
cat(pp("1 + #{x} = #{1 + x}")) # "1 + 1 = 2"
cat(pp(1:3, " are numbers")) # "123 are numbers"
cat(pp(1:3, " are numbers", collapse = " and "))
# "1 and 2 and 3 are numbers"
cat(pp("x = #{x}", "y = #{y}", "x + y = #{x + y}", sep = ", "))
# x = 1, y = 2, x + y = 3

robertzk/Ramd documentation built on May 27, 2019, 10:33 a.m.