Title: | Transform Functions using Decorators |
---|---|
Description: | Instead of nesting function calls, annotate and transform functions using "#." comments. |
Authors: | Nathan Teetor [aut, cre] |
Maintainer: | Nathan Teetor <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.1 |
Built: | 2024-11-05 05:27:39 UTC |
Source: | https://github.com/nteetor/tinsel |
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)
.
# 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.
Get the decorators of a function or the original decoratee function from a decorated function object.
decorators(f) original(f)
decorators(f) original(f)
f |
A decorated function. |
source_decoratees(tinsel_example('attributes.R')) # sourced from the 'attributes.R' example file selector1 # get a list of decorators wrapping a function decorators(selector1) # get the original decoratee function of the # decorated `selector1` function original(selector1)
source_decoratees(tinsel_example('attributes.R')) # sourced from the 'attributes.R' example file selector1 # get a list of decorators wrapping a function decorators(selector1) # get the original decoratee function of the # decorated `selector1` function original(selector1)
Returns TRUE
if the function f
is decorated, otherwise
FALSE
.
is.decorated(f)
is.decorated(f)
f |
A function. |
TRUE
or FALSE
.
source_decoratees(tinsel_example('timer.R')) # sourced from the timer.R example file is.decorated(waldo) is.decorated(jack) # it's a function, but not decorated is.decorated(mean) # far from the mark is.decorated(3030)
source_decoratees(tinsel_example('timer.R')) # sourced from the timer.R example file is.decorated(waldo) is.decorated(jack) # it's a function, but not decorated is.decorated(mean) # far from the mark is.decorated(3030)
The print.decorated
function naively prints x
as a function. In
reality, the function printed may be the final of any number of decorators to
a decoratee. To get the original function or the decorators wrapping it use
original
and decorators
.
## S3 method for class 'decorated' print(x, ...)
## S3 method for class 'decorated' print(x, ...)
x |
A decorated function. |
... |
Additional arguments for next |
source_decoratees(tinsel_example('tags.R')) print(html_paragraph) print(html_bold)
source_decoratees(tinsel_example('tags.R')) print(html_paragraph) print(html_bold)
Given a file, source_decoratees
reads and parses decorated functions
(decoratees) into the calling environment.
source_decoratees(file)
source_decoratees(file)
file |
A character string specifying a file path. |
Malformed decoratees are ignored and a message will alert the user a function has been skipped. However, an error is raised if a decorator is undefined.
If you are working within RStudio the "Source Active File Decoratees" addin
effectively allows you to bind source_decoratees
to a keyboard
shorcut. The addin is found under Tools > Addins.
# source example files source_decoratees(tinsel_example('attributes.R')) source_decoratees(tinsel_example('tags.R')) # the important thing is to look at the contents # of the example files, note the use of the special # "#." comment writeLines(readLines(tinsel_example('attributes.R'))) writeLines(readLines(tinsel_example('tags.R'))) # the decorator functions are not sourced, exists('attribute') # FALSE exists('html_wrap') # FALSE # only decorated functions are sourced print(selector1) selector1(mtcars, 'mpg') # format with bold tags html_bold('make this bold') # format with paragraph tags html_paragraph("I'll make my report as if I told a story...")
# source example files source_decoratees(tinsel_example('attributes.R')) source_decoratees(tinsel_example('tags.R')) # the important thing is to look at the contents # of the example files, note the use of the special # "#." comment writeLines(readLines(tinsel_example('attributes.R'))) writeLines(readLines(tinsel_example('tags.R'))) # the decorator functions are not sourced, exists('attribute') # FALSE exists('html_wrap') # FALSE # only decorated functions are sourced print(selector1) selector1(mtcars, 'mpg') # format with bold tags html_bold('make this bold') # format with paragraph tags html_paragraph("I'll make my report as if I told a story...")