Movatterモバイル変換


[0]ホーム

URL:


Title:Transform Functions using Decorators
Version:0.0.1
Description:Instead of nesting function calls, annotate and transform functions using "#." comments.
License:MIT + file LICENSE
URL:https://github.com/nteetor/tinsel
BugReports:https://github.com/nteetor/tinsel/issues
Encoding:UTF-8
RoxygenNote:5.0.1
Depends:R (≥ 3.3.1)
Suggests:testthat, rstudioapi
NeedsCompilation:no
Packaged:2016-11-16 18:07:32 UTC; nathanteetor
Author:Nathan Teetor [aut, cre]
Maintainer:Nathan Teetor <nathanteetor@gmail.com>
Repository:CRAN
Date/Publication:2016-11-17 08:27:54

tinsel: Transform Functions using Decorators

Description

tinsel provides a decorator syntax for R allowing decoration andtransformation of functions using#. comments.

Details

To the package in action save the code snippet below to a file, runsource_decoratees on the file, and then calltmbg() orcats(5).

# emphasize textemph <- function(f, style = '**') {  function(...) {    if (length(style) == 1) {      paste(style, f(...), style)    } else {      paste(style[1], f(...), style[2])    }  }}#. emphtmbg <- function() {  'tmbg are okay'}#. emph(c('<b>', '</b>'))cats <- function(n) {  paste(rep('cats', n), collapse = ' ')}

The call you make totmbg is equivalent toemph(tmbg). Thesecond example,cats(5), illustrates passing arguments to thedecorator function.

While the above examples are small hopefully you begin to see how decoratorsmay be used to transform or ensure function output without modifying thefunction's code by hand.


Get Function Decorators or Original Function

Description

Get the decorators of a function or the original decoratee function from adecorated function object.

Usage

decorators(f)original(f)

Arguments

f

A decorated function.

Examples

source_decoratees(tinsel_example('attributes.R'))# sourced from the 'attributes.R' example fileselector1# get a list of decorators wrapping a functiondecorators(selector1)# get the original decoratee function of the# decorated `selector1` functionoriginal(selector1)

Decorated Functions

Description

ReturnsTRUE if the functionf is decorated, otherwiseFALSE.

Usage

is.decorated(f)

Arguments

f

A function.

Value

TRUE orFALSE.

Examples

source_decoratees(tinsel_example('timer.R'))# sourced from the timer.R example fileis.decorated(waldo)is.decorated(jack)# it's a function, but not decoratedis.decorated(mean)# far from the markis.decorated(3030)

Print a Decorated Function

Description

Theprint.decorated function naively printsx as a function. Inreality, the function printed may be the final of any number of decorators toa decoratee. To get the original function or the decorators wrapping it useoriginal anddecorators.

Usage

## S3 method for class 'decorated'print(x, ...)

Arguments

x

A decorated function.

...

Additional arguments for nextprint method.

Examples

source_decoratees(tinsel_example('tags.R'))print(html_paragraph)print(html_bold)

Read and Parse Decoratees from a File

Description

Given a file,source_decoratees reads and parses decorated functions(decoratees) into the calling environment.

Usage

source_decoratees(file)

Arguments

file

A character string specifying a file path.

Details

Malformed decoratees are ignored and a message will alert the user a functionhas been skipped. However, an error is raised if a decorator is undefined.

If you are working within RStudio the "Source Active File Decoratees" addineffectively allows you to bindsource_decoratees to a keyboardshorcut. The addin is found underTools >Addins.

Examples

# source example filessource_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# "#." commentwriteLines(readLines(tinsel_example('attributes.R')))writeLines(readLines(tinsel_example('tags.R')))# the decorator functions are not sourced,exists('attribute')  # FALSEexists('html_wrap')  # FALSE# only decorated functions are sourcedprint(selector1)selector1(mtcars, 'mpg')# format with bold tagshtml_bold('make this bold')# format with paragraph tagshtml_paragraph("I'll make my report as if I told a story...")

Get Path of a Tinsel Example File

Description

tinsel_example simplifies getting and returns the system path of anexample file included in the tinsel package. To list the names of all examplefiles usetinsel_examples.

Usage

tinsel_example(path)tinsel_examples()

Arguments

path

Name of the example file.

Examples

# list all example filestinsel_examples()# get the path of a specific exampletinsel_example('attributes.R')

[8]ページ先頭

©2009-2025 Movatter.jp