tweak | R Documentation |
Easily manipulate a plot using controls like sliders, drop-down lists and date pickers.
tweak(expr, ..., options = list(), .envir = parent.frame())
expr |
an expression that evaluates to a plot using base plotting
functions, |
... |
variables within the
|
options |
a
|
.envir |
environment in which to evaluate |
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.