A first go at ‘manipulate’ in RStudio

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:

Select All Code:
# Adapting Richie Cotton's gWidgettcltk example to RStudio's manipulate
# updated 10/11/2012 to reflect changes in ggplot2
# C. Ladroue
 
library("ggplot2")
library("manipulate")
 
chromium <- read.csv("chromium.csv")
nickel <- read.csv("nickel.csv")
 
manipulate({
  p<- ggplot(data, aes(air, bm)) + geom_point()+scale_x_continuous(trans=xScale)+scale_y_continuous(trans=yScale)
  if(facet!="None") p<-p+facet_grid(facet)
  p
},
           yScale=picker("Linear"="identity","Log"="log10",label="Y Scale Transformation"),
           xScale=picker("Linear"="identity","Log"="log10",label="X Scale Transformation"),
           facet=picker( "None" = "None", "RPE" = ". ~ rpe","Welding type" = ". ~ welding.type","RPE and Welding type" = "rpe ~ welding.type",initial="None",label="Faceting"),
           data=picker("Chromium"=chromium,"Nickel"=nickel,label="Datasets")
)

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

Select All Code:
data=picker("Chromium"=chromium,"Nickel"=nickel,label="Datasets")

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.

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

4 Responses to A first go at ‘manipulate’ in RStudio

  1. Thanks for the mention.

    I hadn’t come across manipulate before; I agree that it’s very easy to get started with, and fast. Once they have a few more controls available I think it will be a very useful tool.

  2. CL says:

    Thanks for your post in the first place!

    Manipulate is indeed fast to set up and I’m sure it’s possible to write a version of it in TclTk to bypass the RStudio requirement. I’ll leave that to others to write up though.

  3. Jon says:

    Looks really very useful, but could not not get it to work with the following error:

    Error: Must specify at least one variable to facet by

    • CL says:

      Thank you for your comment!
      I looked at it and the error comes from a syntax change in ggplot2. I’ve updated the code to reflect that and it’s working again.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


1 + = ten

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">