statar (version 0.1.1)

pastem: String and expression interpolation

Description

String and expression interpolation

Usage

pastem(..., sep = " ", pattern = "$", parenthesis.only = FALSE,
  env = parent.frame(), inherits = FALSE)

pastem0(..., env = parent.frame())

evalm(x, pattern = "$", parenthesis.only = FALSE, env = parent.frame(), inherits = FALSE)

quotem(x, pattern = "$", parenthesis.only = FALSE, env = parent.frame(), inherits = FALSE)

quotem_(x, pattern = "$", parenthesis.only = FALSE, env = parent.frame(), inherits = FALSE)

Arguments

...
Paste multiple strings
sep
a character string to separate the terms.
pattern
pattern to use. Default to $
parenthesis.only
Limit patterns within parenthesis?
env
environment in which to evalute the expressions enclosed in patterns. Default to current environement
inherits
Default to FALSE
x
any syntactically valid R expression

Value

  • The functions pastem does string interpolations. The functions quotem does expression interpolations. It is very similar to `substitute` except that (i) it works in the global environment (ii) only variables prefixed with a pattern are substituted (iii) LHS of `=` is also substituted. The function evalm is a wapper for eval(quotem())).

    The expressions includes all letters + underscore that follow the pattern. Use parenthesis if you want the expression to be shorter or longer. Expressions that follow the pattern are evaluated in the environment specified by env and substituted into the original object. When they are the name of a non existent object, nothing is returned. Note that in the expression list(`$ok`="`$ok`"), both occurences are substituted by the object to which ok refers to.

Details

The functions replaces expressions starting with the pattern by evaluating them in the environment specified by env (susbtituting by nothing if not found)

Examples

Run this code
height <- 72
units <- "inches"
weight <- 230
a <- "ght"
pastem("My record indicates you are $height $(units).")
pastem("Your body mass index is $(round(703*weight/height^2))")
pastem("My record indicates you are $(hei$a) inches tall")
pastem("You are .(height) inches tall.This is below average", pattern = ".")
a <- ".This"
pastem("You are .(height) inches tall.a is below average", pattern = ".")
library(data.table)
N <- 100
DT <- data.table(
  id = sample(5, N, TRUE),
  v1 = sample(5, N, TRUE),
  v2 = sample(1e6, N, TRUE)
)
newvar <- quote(temp)
myvar <- quote(v1)
byvar <- c("id", "v1")
quotem(DT[, list(`$newvar` = mean(`$myvar`)), by = `$byvar`])
evalm(DT[, list(`$newvar` = mean(`$myvar`)), by = `$byvar`])

Run the code above in your browser using DataCamp Workspace