db.subst: Simple expression substitution within strings

View source: R/db-subst.R

db.substR Documentation

Simple expression substitution within strings

Description

Simple expression substitution within strings

Usage

db.subst(db, text, env = parent.frame())

Arguments

db

The database connection.

env

The environment in which to perform the substitution.

txt

The character string for substitution.

Details

The input text is searched for text enclosed between < in the calling frame, substituting the value of the last evaluated expression (if it is a string) in place of the text between delimiters.

Value

The input character string with the values returned from any expressions between braces substituted in place.

Note

Currently delimiters do not nest. That is, the function will stop at the first closing delimiter sequence it finds and try to parse the extracted expression. So "hello, < which will return a parse error.

Examples

db = db.open()
greeting = "world!"
db.subst(db, "hello, <% greeting %>!") # => hello, world!!
db.subst(
  db
  , "hello, <%greeting%> what a grand <%greeting%>"
) # => hello, world! what a grand world!
bar = function() return ("bar")
db.subst(db, "foo <% bar() %>") # => foo bar
db.subst(db, "foo <% bar %>") # => foo # note a trailing space
db.subst(db, "foo <% paste0(deparse(bar), collapse='') %>") # => foo function () return(\"bar\")
db.close(db)

blueraleigh/db documentation built on Feb. 25, 2024, 9:13 a.m.