Christophe Ladroue » R, and kindly contributed to Want to share your content on R-bloggers? [This article was first published on, and kindly contributed to R-bloggers ]. (You can report issue about the content on this page here Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Something I’m missing from R (especially coming from Mathematica) is the ability to quickly build interactive graphs, which I find very useful for getting a good intuition of the impact of parameters on a mathematical function.

Richie Cotton’s post about interactive plots in R gave me an incentive to have a go at the manipulate package in RStudio.

I adapted Richie’s example (go to his page to download his data and example) to manipulate and I have to say I have been impressed by how easy and fast it is to put something together.

Here is the whole script that replicates the example:

Selec All Code: # Adapting Richie Cotton's gWidgettcltk example to RStudio's manipulate # C. Ladroue library("ggplot2") library("manipulate") chromium

And the result is as expected: a graph with added controls. (click to view the whole image)



As you can see from the code, this is extremely readable. The only control type I’ve had to use is picker , which takes a list of labels, followed by an optional value. It’s this value that will be passed on to the assigned variable. The label appearing on the form can also be changed with label= . Other available controls are slider and checkbox . There is no text control at the moment, so I couldn’t replicate the ‘title’ box from Richie’s example.

I’ve used the TclTk package before and while it worked, it was a bit too cumbersome to be used routinely. By contrast, manipulate allows for a very easy and fast setup, with little to no overhead. This comes at the expense of being restricted to RStudio, but since this is my IDE of choice, I’m fine with that.

This is only a quick and dirty attempt at manipulate and there are things I’d like to change; in particular, I’m fairly sure that the line

is an unnecessary memory hog, due to the passing of the 2 data frames to picker . A better choice would be to only pass a string and check its value in the expression that generates the plot.