Learn R Programming

reportr (version 1.1.2)

s: Simple expression substitution

Description

Evaluate R expressions and substitute their values into a character vector.

Usage

s(strings, round = NULL, signif = NULL, envir = parent.frame())

Arguments

strings
A character vector containing the strings to substitute into, each containing any number of "#{}" blocks.
round
NULL or a single integer, giving the number of decimal digits for rounding numeric expressions. This argument takes priority over signif.
signif
NULL or a single integer, giving the number of significant decimal digits to use for numeric expressions. The round argument takes priority over this one and will be used if not NULL.
envir
The environment to evaluate expressions in.

Value

  • A character vector containing the final strings, with expressions substituted into them.

Details

Each part of each string surrounded by "#{}" is extracted, evaluated as R code in the specified environment, and then its value is substituted back into the string. The literal string "#{}" can be obtained by escaping the hash character, viz. "\\#{}". The block may contain multiple R expressions, separated by semicolons, but may not contain additional braces. If the reportrNumericRepresentation option is defined and is a list, its round and signif elements provide defaults for the arguments of the same name.

See Also

report

Examples

Run this code
s("pi is #{pi}")
s("pi is \\#{pi}")
s("The square-root of pi is approximately #{sqrt(pi)}", signif=4)
s("1/(1+x) for x=3 is #{x <- 3; 1/(1+x)}")

Run the code above in your browser using DataLab