Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Use Decorators to Transform Functions

License

NotificationsYou must be signed in to change notification settings

nteetor/tinsel

Repository files navigation

Decorating functions in R.

Travis-CI Build Statuscodecovcran status

The tinsel package adds function decorators to R using a special#. comment.Decorators are a means of transforming a function without needing to rewritethe function. They allow for easy integration of new code onto existing code. These benefitsare illustrated below with an example about object classes.

What are decorators all about?

Say we develop aSpaceship class. In addition to our standardSpaceshipclass, we also need a class for a spaceship with a hyperdrive. So we develop aSpaceshipWithHyperdrive class. Hoever, given all our spaceships we also need amothership. So we develop aMotherSpaceship. But, what if the mothership alsohas a hyperdrive? In this have to add two new classes,MotherSpaceship andMotherSpaceshipWithHyperdrive. While the situation is not unmanageable addinga new class for each spaceship feature, or even for every couple of features, isless than ideal.

Instead we can create a decorator for each new spaceship feature. Let's callthese decoratorsHyperdriveSpaceshipDecorator andMotherSpaceshipDecorator.TheHyperdriveSpaceshipDecorator takes theSpaceship class, with all theSpaceship methods, and adds hyperdrive-related methods. Thus, we are saved thetrouble of copying over theSpaceship methods to a new class as would havebeen necessary to create theSpaceshipWithHyperdrive,MotherSpaceship,andMotherSpaceshipWithHyperdrive classes.

R Decorators (andyour decorators)

Let's create a functionif_warning which wraps a functionf such that iff(...)would generate a warning a default value is returned instead, otherwisef(...) isreturned.

if_warning<-function(f,default) {function(...) {    tryCatch(      f(...),warning=function(w) {default      })  }}

Now let's transform the defaultmean function, so instead of generating awarning the function returnsInf. Great!

mean_inf<- if_warning(mean,Inf)# give it a try!mean_inf(1:5)mean_inf(c(1,'two',3))

Here is where the special comment#. comes in. The above code can berewritten as the following,

#. if_warning(Inf)mean_inf<-mean

The special comment#. is used to denote a function decorator. In thisexample,#. if_warning denotesif_warning as the decorator of the decorateemean.mean is passed as the first argument toif_warning andInfas the second argument. The result of transformingmean withif_warning isassigned tomean_inf.

In order to see the decorator annotation example in action, save the above codein a file, source the file usingsource_decoratees, and then callmean_infonce more. (Gentle reminder, the output is expected to be the same)

Installing tinsel

tinsel is now available on CRAN.

install.packages('tinsel')

You can install the latest version of tinsel using devtools (currently even with version 0.0.1 on CRAN).

# install.packages('devtools')devtools::install_github('nteetor/tinsel')

Check out thesource_decoratees function to get started.

RStudio Addin

If you are working in RStudio the tinsel package includes an addin for the corefunctionsource_decoratees. To bind the addin to a keyboard shortcut inRStudio navigate toTools >Addins >Browse Addins >KeyboardShorcuts. For more information about the keyboard shortcuts checkout theRStudiosupportpage.If you choose to setup a keyboard shortcut for the addin I recommendAlt+Shift+S sinceCmd+Shift+S (orCtrl+Shift+S on Linux and Windows) is the source active fileshortcut. The end result is you can quickly load your decorated functions likeyou would source all functions from the active file.


Cheers, Nate.

About

Use Decorators to Transform Functions

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2026 Movatter.jp