Learn R Programming

Ramd (version 0.2)

pp: Ruby-like string interpolation

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

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

Run this code
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

Run the code above in your browser using DataLab