R + Shiny + image plots: no frills example

I’ve uploaded a new R Shiny web application. This app shows plots of bootstrapped, 50-year moving average correlations between tree growth and both temperature and precipitation at various sites. But that nature of the data is not the topic here. Some of my apps can be kind of complex and large. However, I decided to throw this one together as a simpler example. The matrices are formatted and stored in an R workspace file. There is one menu for selecting which image to plot.

Perhaps the most beneficial aspect to this example app is that even though it is very simple, I used one of my more complex apps as a template. In general the Shiny apps I make involve a lot of code externalization, files sourcing other files hierarchically. I find this makes it easier to develop more complex apps, and more easily manage the unpredictable growth in complexity of an app if in the course of development I need to add features I couldn’t plan for in advance or others make requests for features later on. It can be a bit challenging to wrap your head around the plethora of files at a glance, but after playing connect the dots among the different R scripts in the app, you start to see the hierarchy. It’s overkill for this app, but that’s what allows you to see the structure without the complexity getting in the way.

My general approach is to never touch the ui.R and server.R scripts. They only source other files. For instance the former will source scripts for the header, sidebars and main panel. The latter will source scripts containing all my reactive expressions, plotting functions, etc., mainly through an app.R file which, in turn, sources the most external files. There is nothing special about how I chose to generalize or externalize my code. I know that others do it differently, and I am always looking for even better methods of app organization. So if you have any suggestions for improvements or just want to share your own approach, please let me know.

Lastly, I shouldn’t say “no frills”. The image plots do involve custom code wrapped around the base image function. I’m a stickler for nice formatting and image alone was not going to do it. And image.plot from the fields package was easier to work with but still too limiting. Some suggest combining the two, the first for the main graphic and the second for the legend. But I am just never satisfied with the legend; I have no idea why, but the color key bar in image.plot graphics never manage to show color segments for each break of equal width. When I wrote my own wrapper function this was pretty easy to achieve. As usual, the code is available on github. Just visit the app above, click on the About tab, and the github link is at the bottom.

This entry was posted by Matt Leonawicz.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: