Introducing Rsenal – magic R functions for things various

Today, I would like to quickly introduce our package “Rsenal”. It is basically a collection of R functions that we use in our daily lives at Environmental Informatics Marburg. The package is hosted at GitHub and can be installed using the install_github() function from package devtools.

This package is the opposite of a 'general purpose' package, yet there are some functions that may be of interest to the wider R user community, for example latticeCombineGrid() and latticeCombineLayer(). The idea behind these functions is to provide tools to easily combine lattice plot objects that are stored in a list. These may, for example, result from iterative processes using lapply().

Below is an example for latticeCombineGrid() using the panel.2dsmoother() example from latticeExtra modified such that we loop over four different standard deviation values in a lapply() loop.


sds <- c(1, 2, 3, 4)

clrs <- colorRampPalette(brewer.pal(9, "RdBu"))

## example taken from 
## looping over 4 different standard deviations - sds
p_list <- lapply(seq(sds), function(i) {
  xyz <- data.frame(x = rnorm(100), y = rnorm(100))
  xyz$z <- with(xyz, x * y + rnorm(100, sd = sds[i]))

  p <- levelplot(z ~ x * y, xyz, panel = panel.2dsmoother,
                 col.regions = clrs(1000), at = seq(-5, 5, 0.1),
                 aspect = 1)

p_final <- latticeCombineGrid(p_list)

plot of chunk unnamed-chunk-1

By default, panels are separated slightly (which I find a little more optically pleasing) and plotting order is from top left to bottom right (for lattice the default is bottom left to top right). Under the hood, Reduce() is used to combine the plot objects stored in a list.

latticeCombineLayer() works in a similar way, but lets you plot the different objects on top of each other.

Another useful function for package developers is bumpVersion() which lets you modify the Version: number and automatically changes the Date: entry in in your package DESCRPTION file. That said, it assumes that you use semantic software versioning. I usually commit all changes I made first and then bumpVersion() in a separate commit.

I hope that some people may find the fucntion collection in Rsenal useful.

Please provide feedback, feature requests, suggestions and bug reports here.

This entry was posted in R. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s