# A first go at ‘manipulate’ in RStudio

**Christophe Ladroue » R**, 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:

# Adapting Richie Cotton's gWidgettcltk example to RStudio's manipulate # 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() p+facet_grid(facet)+scale_x_continuous(trans=xScale)+scale_y_continuous(trans=yScale) }, yScale=picker("Linear"="identity","Log"="log10",label="Y Scale Transformation"), xScale=picker("Linear"="identity","Log"="log10",label="X Scale Transformation"), facet=picker( "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

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.

**leave a comment**for the author, please follow the link and comment on their blog:

**Christophe Ladroue » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.