R: Create an interactive plot

manipulate {manipulate} | R Documentation

## Create an interactive plot

### Description

The `manipulate` function accepts a plotting expression and a set of controls (e.g. `slider`, `picker`, `checkbox`, or `button`) which are used to dynamically change values within the expression. When a value is changed using its corresponding control the expression is automatically re-executed and the plot is redrawn.

### Usage

    manipulate(`_expr`, ...)

### Arguments

`_expr` | Expression to evalulate. The expression should result in the creation of a plot (e.g. `plot` or `qplot`). Note that the expression need not be a top-level plotting function, it could also be a custom function that creates a plot as part of its implementation. This expression will be re-evaluated with appropriate parameter substitution each time one of the manipulator control values is changed.
`...` | One or more named control arguments (i.e. `slider`, `picker`, `checkbox`, or `button`), or a list containing named controls.

### Details

Once a set of manipulator controls are attached to a plot they remain attached and can be recalled whenever viewing the plot (a gear button is added to the top-left of the plot to indicate that it has a manipulator).

The `_expr` argument is evaluated using `withVisible`. If it's return value is visible then `print` is called. This enables manipulate expressions to behave simillarly to their being executed directly at the console.

The `_expr` argument uses a syntactially invalid (but backtick quoted) name to avoid clashes with named control arguments.

The `manipulatorSetState` and `manipulatorGetState` functions can be used to associate custom state with a manipulator (for example, to track the values used for previous plot executions). These values are stored in a custom environment which is stored along with the rest of the manipulator context.

### Examples

    ## Not run:

    ## Create a plot with a manipulator
    manipulate(plot(1:x), x = slider(5, 10))

    ## Using more than one slider
    manipulate(
      plot(cars, xlim=c(x.min,x.max)),
      x.min=slider(0,15),
      x.max=slider(15,30))

    ## Filtering data with a picker
    manipulate(
      barplot(as.matrix(longley[,factor]),
              beside = TRUE, main = factor),
      factor = picker("GNP", "Unemployed", "Employed"))

    ## Create a picker with labels
    manipulate(
      plot(pressure, type = type),
      type = picker("points" = "p", "line" = "l", "step" = "s"))

    ## Toggle boxplot outlier display using checkbox
    manipulate(
      boxplot(Freq ~ Class, data = Titanic, outline = outline),
      outline = checkbox(FALSE, "Show outliers"))

    ## Combining controls
    manipulate(
      plot(cars, xlim = c(x.min, x.max), type = type,
           axes = axes, ann = label),
      x.min = slider(0,15),
      x.max = slider(15,30, initial = 25),
      type = picker("p", "l", "b", "c", "o", "h", "s", "S", "n"),
      axes = checkbox(TRUE, "Draw Axes"),
      label = checkbox(FALSE, "Draw Labels"))

    ## End(Not run)

[Package manipulate version 1.0.1 Index]