tweak: Manipulate a plot

View source: R/tweak.R

tweakR Documentation

Manipulate a plot

Description

Easily manipulate a plot using controls like sliders, drop-down lists and date pickers.

Usage

tweak(expr, ..., options = list(), .envir = parent.frame())

Arguments

expr

an expression that evaluates to a plot using base plotting functions, ggplot, etc.

...

variables within the expr expression to be manipulated. These can be specified in one of two ways:

The easy way

The easy way is to specify the variables to be manipulated as named arguments to tweak. How you specify the value of each argument determines the default value of the variable and how it is manipulated. Examples for each:

  • x = c(min, max) for a numeric slider between min and max; you can optionally provide a starting value before min and/or a step value after max (see examples).

  • y = list(...) for a fixed set of options in a dropdown menu. If the list has names, these will be shown. The first element is selected by default. For convenience, a character vector with more than one element will also be interpreted as a dropdown menu.

  • z = TRUE or z = FALSE for a logical value controlled by a checkbox.

  • foo = "Some text" for a character string controlled by text input.

  • bar = 123.456 for a numeric value controlled by text input.

  • baz = as.Date("2020-01-01") for a Date object with a calendar input.

See below for an example.

The more flexible way

The more flexible way is to specify the variables to be manipulated as input controls using the shiny package. In this case the names of the arguments are ignored, and the variable names are taken from the inputId argument to the Shiny input control. An example is below.

options

a list containing further settings:

position

where the controls are positioned relative to the plot; either "bottom" (default), "top", "left", or "right".

ncol

if position is "top" or "bottom", the number of columns to distribute controls across; can be 1 (default), 2, 3, or 4.

gadget

FALSE (default) to run in a new window, or TRUE to run as a gadget, i.e. in the RStudio viewer pane.

plot_height

Height of the plot in pixels, with 400 as the default.

.envir

environment in which to evaluate expr.

Examples

## Not run: 
# Specifying controls: the easy way
tweak({
        x = 0:10;
        plot(A * x^2 + B * x + C,
            col = if (blue) "blue" else "black",
            main = plot_title,
            ylim = c(-5, 10)
        )
    },
    A = c(0, 0.1), # slider from 0 to 0.1
    B = 1,         # numeric text input with starting value 1
    C = list(one = 1, two = 2, three = 3), # dropdown list with named values
    plot_title = "Example title", # freeform text input
    blue = FALSE                  # checkbox
)

# Specifying controls: the flexible way
library(shiny)
library(ggplot2)

tweak({
        dat = data.frame(
            date = start_date + 0:(n_days - 1),
            value = start_value * exp(0:(n_days - 1) * growth_rate) +
                rnorm(n_days, 0, noise)
        )
        ggplot(dat) +
            geom_line(aes(x = date, y = value))
    },
    dateInput(inputId = "start_date",
        label = "Start date", value = "2020-01-01"),
    numericInput(inputId = "start_value",
        label = "Starting value", value = 1, min = 0, max = 10, step = 1),
    sliderInput(inputId = "growth_rate",
        label = "Growth rate", min = 0, max = 1, value = 0, step = 0.01),
    numericInput(inputId = "n_days",
        label = "Number of days", value = 30, min = 1, max = 60, step = 1),
    sliderInput(inputId = "noise",
        label = "Noise", min = 0, max = 1, value = 0, step = 0.01)
)

# Different kinds of numeric sliders
tweak({ x = 0:100; plot(A * x^2 + B * x + C, ylim = c(-2000, 2000)) },
    A = c(0.5, 0, 1),         # slider from 0 to 1, with starting value 0.5
    B = c(0, 10, 0.25),       # slider from 0 to 10, with step 0.25
    C = c(0, -1000, 1000, 50) # slider from -1000 to 1000, starting value 0 and step 50
)

# tweak plus curve
tweak(curve(dbeta(x, alpha, beta), 0, 1), alpha = c(1, 100), beta = c(1, 100))

# Quickly explore a numeric data.frame
data(quakes)
tweak(
    if (x == y) {
        hist(quakes[[x]], xlab = x)
    } else {
        plot(quakes[[x]], quakes[[y]], xlab = x, ylab = y)
    },
    x = names(quakes), y = names(quakes))

## End(Not run)

nicholasdavies/shmanipulate documentation built on July 16, 2025, 5:39 p.m.