tinsel provides a decorator syntax for R allowing decoration and
transformation of functions using #.
comments.
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)
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # 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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.