Programming inter-activities in R with twiddler

Hey there R user! Where you ever jealous of how easy it is to create interactive programs in mathematica/python/matlab/etc compared to R? I certainly  was. Luckily,there is at least one library that gives R this functionality and it is called twiddler.

The main command of twiddler is twiddle. Here’s the description.

twiddle automatically creates a GUI for interactively manipulating variables in an R expression expr. This GUI will contrain a control, i.e. a slider or a checkbox, for every unbound variable in expr. expr is re-evaluated every time the user changes a control after binding the unbound variables in expr to the new settings of their associated controls. This allows easy interactive exploration of parameter spaces.

Using twiddle

It’s actually really simple. The most basic use of  it is just passing an R command. Then twiddle will automatically open a GUI which will control the command. By default, there will be one slider per  unknown parameter.

After you install twiddler, try the following command.


x = seq(0,2*pi,length=100);


In the above code, a and b don’t have any values yet and thus twiddler assumes they are free parameters and creates a slider for each one.

Twiddle controls

Moving the sliders will update the values of a and b and the plot command. However, the default values feel a little awkward and may not be the ones we want.  twiddle comes with many different options for customizing the controls. For example, we can change the range of the sliders above with the following code.



This results to the following (expected) change.

More power over the controls.

Beyond the knob

Twiddler has way more options than just using knobs to change values. You can have preselected options for the user to pick from or empty text boxes where someone can enter information through. One of the examples in the documentation deals with boolean values.

twiddle(print(a && !b), a = toggle(), b = toggle())


This generates the following selection box, with a and b having true or false values.


After every choice by the user, the current value of a&& !b is posted in the console (due to print()).

Beyond “one function” commands

All of our inter-activities up to this point where simple commands. Twiddle just had to process one call of a simple function. In case you want to program something a bit more complicated, you can just wrap it in a function and then pass the function to twiddle. For example,

sinIter <- function(x){
y = x;
for(i in 1:100){
y = sin(y)


This code creates a GUI through which we can input various values to the iterated sine function. Every time we change the value of x, we see the new value of the iterated sine in the console.

Iterated Sine

I used twiddler to create an interactive lab exercise for a class and I have to say that I really liked how easy it was to set it up and use it. To be fair, it wasn’t easier than mathematica’s “Manipulate” but the end result was great for a “free to use” package. 🙂

More about twiddler here.