[R] decorating API in R

Tom Roche Tom_Roche at pobox.com
Fri Nov 9 22:10:19 CET 2012


How best to implement a decorator pattern in R? What I mean, why I ask:

A group of ncdf4 users is trying to make it easier (notably for ourselves :-) to write correctly IOAPI-formatted netCDF. Since IOAPI

http://www.baronams.com/products/ioapi/

decorates netCDF, one obvious way to do a package=ioapi is to decorate the netCDF API from package=ncdf4. E.g., to add a data variable (datavar) with the additional IOAPI metadata, ioapi::ncvar_add(...) would call ncdf4::ncvar_add(...), then (e.g.--not complete, strictly for illustration)

* create global attr=VAR-LIST if it does not already exist
* add properly-formatted variable name to VAR-LIST
* add properly-formatted attrs longname, units, var_desc to the datavar if they do not already exist

I've spent most of my codelife in Java, where one can decorate as above either

* @ compile-time: simple inheritance. Brittle longterm, but gets the job done for now.

* @ run-time: real Decorator Pattern à la the Gang of Four. Not much harder, usually worth the investment, but not "the simplest thing that could possibly work," either.

But, given my ignorance of the R object models and dependency management, before I put much scarce time into researching this, I thought I'd ask the community for suggestions, references, and especially examples for how to do this in R, especially how to extend one R package with another.

TIA, Tom Roche <Tom_Roche at pobox.com>



More information about the R-help mailing list