tinsel-package {tinsel} | R Documentation |
tinsel: Transform Functions using Decorators
Description
tinsel provides a decorator syntax for R allowing decoration and
transformation of functions using #.
comments.
Details
To the package in action save the code snippet below to a file, run
source_decoratees
on the file, and then call tmbg()
or
cats(5)
.
# emphasize text emph <- function(f, style = '**') { function(...) { if (length(style) == 1) { paste(style, f(...), style) } else { paste(style[1], f(...), style[2]) } } } #. emph tmbg <- function() { 'tmbg are okay' } #. emph(c('<b>', '</b>')) cats <- function(n) { paste(rep('cats', n), collapse = ' ') }
The call you make to tmbg
is equivalent to emph(tmbg)
. The
second example, cats(5)
, illustrates passing arguments to the
decorator function.
While the above examples are small hopefully you begin to see how decorators may be used to transform or ensure function output without modifying the function's code by hand.
[Package tinsel version 0.0.1 Index]