Package 'templates'

Title: A System for Working with Templates
Description: Provides tools to work with template code and text in R. It aims to provide a simple substitution mechanism for R-expressions inside these templates. Templates can be written in other languages like 'SQL', can simply be represented by characters in R, or can themselves be R-expressions or functions.
Authors: Sebastian Warnholz [aut, cre]
Maintainer: Sebastian Warnholz <[email protected]>
License: MIT + file LICENSE
Version: 0.4.0
Built: 2024-11-10 04:15:02 UTC
Source: https://github.com/wahani/templates

Help Index


Template constructors

Description

tmpl is the constructor function for template objects.

Usage

tmpl(.t, ...)

## S3 method for class 'character'
tmpl(.t, ..., .envir = parent.frame())

## S3 method for class 'formula'
tmpl(.t, ...)

## S3 method for class 'tmpl'
tmpl(.t, ...)

## S3 method for class 'function'
tmpl(.t, ...)

Arguments

.t

something that can be interpreted as template. See defined methods for options.

...

(name = value | name ~ value) name-value expressions passed on to tmplUpdate

.envir

(environment) the environment in which template snippets are evaluated. For formulas and functions their environment is used.

Details

Objects of class tmpl are stored as a character of length one. They can contain 'snippets' to be evaluated. These snippets are identified by an opening {{ and closing }}. The environment in which they are evaluated is stored in the object. They can be further augmented by supplying arguments in ....

See Also

tmplUpdate, tmplEval

Examples

tmpl("Hi {{ toupper(a) }}!", a = "there")
tmpl( ~ {y <- {{ a }}}, a ~ x + 1)
tmpl(function(x) {{ a }} + x, a ~ 1)

Update and evaluate templates

Description

Functions operating on tmpl objects. They can be updated and / or evaluated as an expression.

Usage

tmplUpdate(.t, ...)

## S3 method for class 'tmpl'
tmplUpdate(.t, ...)

## S3 method for class 'function'
tmplUpdate(.t, ...)

tmplEval(.t, ..., .envir = new.env(parent = parent.frame()))

tmplAsFun(.t, ...)

Arguments

.t

(tmpl) and object of class tmpl

...

(name = value | name ~ value) name-value expressions used to update the snippets in x

.envir

(environment) the environment in which the template is evaluated

Details

tmplUpdate will evaluate all snippets in a template. Objects are searched for in the list of arguments supplied as ... and the environment of the template. The results are substituted within the snippets.

tmplEval will evaluate the template in place or in the specified environment after substituting the elements in ....

Examples

tmpl("This is {{ a }} very similar to {{ b }}", a = "actually", b = "sprintf")
tmpl("But I consider it to be ({{ sprintf('%i', a) }}) orthogonal", a = 1.0)
tmpl("and ({{ sprintf('%i', b) }}) with a different scope:", b = 2.0)
tmpl("SELECT {{ var }} FROM {{ table }} WHERE {{ condition }};",
     var = "someVar", table = "someTable", condition = "primaryKey = 1")
template <- tmpl("cat({{ toupper(x) }})")
tmplUpdate(template, x ~ "hi")
tmplEval(template, x ~ "hi")