Transformers allow you to apply functions to the glue input and output, before and after evaluation. This allows you to write things like glue_sql(), which automatically quotes variables for you or add a syntax for automatically collapsing outputs.

The transformer functions simply take two arguments text and envir, where text is the unparsed string inside the glue block and envir is the execution environment. Most transformers will then call eval(parse(text = text, keep.source = FALSE), envir) which parses and evaluates the code.

You can then supply the transformer function to glue with the .transformer argument. In this way users can define manipulate the text before parsing and change the output after evaluation.

It is often useful to write a glue() wrapper function which supplies a .transformer to glue() or glue_data() and potentially has additional arguments. One important consideration when doing this is to include .envir = parent.frame() in the wrapper to ensure the evaluation environment is correct.

Some examples implementations of potentially useful transformers follow. The aim right now is not to include most of these custom functions within the glue package. Rather users are encouraged to create custom functions using transformers to fit their individual needs.

Shell quoting transformer

A transformer which automatically quotes variables for use in shell commands, e.g. via system() or system2().

sprintf transformer

A transformer which allows succinct sprintf format strings.