Rick Wargo
2016-Feb-22 18:03 UTC
[R-pkgs] New Package: lrequire v0.1.3 - use modules to encapsulate and cache your R scripts
Dear R Users,
I am happy to announce the initial release (v0.1.3) of
lrequire<https://cran.r-project.org/web/packages/lrequire/>, now available
on CRAN.
https://cran.r-project.org/web/packages/lrequire/
lrequire supports modularization of R Scripts, enabling encapsulation of
information and caching of scripts. This is very similar to the
require()<https://nodejs.org/api/modules.html> support in
node.js<https://nodejs.org/>.
Use of lrequire encourages separation of responsibility and using modules to
encapsulate specific functionality. This leads to more easily-maintained scripts
and encourages reuse.
lrequire also cache loaded modules (scripts) such that the next load (source) of
the script does not execute its contents, unless specified. For example, if a
module is built around sourcing a slowly-changing dataset that is time-consuming
to retrieve, frequent reloads of the module will only return the cached copy of
the data, saving the time expense. I found this very useful for developing
dashboard components ? lrequire permitted me the freedom to keep tweaking the UI
without having to wait for the live reload of the data.
How It Works
Goal: build a reusable module to welcome an individual. Any environment
artifacts necessary to build that module are encapsulated in the lrequire() call
and only the functionality is returned.
For example:
________________________________
File: welcome.R
data.that.will.not.be.exposed <- 'some work'
hello <- function(person.name) {
return (paste0('Hello, ', person.name, '!'))
}
module.exports = hello
________________________________
File: main.R
hello <- lrequire(welcome)
hello('Rick')
________________________________
Note that the variable declared in welcome.R will not be visible in main.R only
the hello() function. Multiple pieces of information can be exposed by returning
a list() object in module.exports. Refer to the documentation for details.
All the best,
Rick Wargo
https://linkedin.com/in/rickwargo/
https://www.rickwargo.com/
@rickwargo
[[alternative HTML version deleted]]