Skip to content

Commit 203707b

Browse files
committed
allow autoTheme options to be reactive
1 parent ec8922e commit 203707b

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

R/render-plot.R

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ renderPlot <- function(expr, width='auto', height='auto', res=72, ...,
178178
#' 2. Opt-out of particular auto-theming features (by supplying `NA` to specific
179179
#' option(s)).
180180
#'
181+
#' Options may also be reactive values which, when invalidated, trigger a redraw for
182+
#' any plots that depend on those values.
183+
#'
181184
#' @param bg background color (defaults to the background color of the containing
182185
#' HTML element).
183186
#' @param fg foreground color (defaults to the foreground color of the containing
@@ -194,25 +197,45 @@ renderPlot <- function(expr, width='auto', height='auto', res=72, ...,
194197
#'
195198
#' @export
196199
#' @examples
197-
#' opts <- autoThemeOptions(accent = "red", sequential = NA)
198-
#' shinyOptions(plot.autotheme = opts)
200+
#'
201+
#' library(ggplot2)
202+
#'
203+
#' p <- ggplot(diamonds[sample(nrow(diamonds), 1000), ], aes(carat, price)) +
204+
#' geom_point(alpha = 0.2) +
205+
#' geom_smooth() +
206+
#' facet_wrap(~cut) + ggtitle("Diamond price by carat and cut")
207+
#'
208+
#' base_colors <- tags$style(HTML("body{background-color:#444; color:#e4e4e4}"))
209+
#'
210+
#' if (interactive()) {
211+
#' shinyApp(
212+
#' fluidPage(base_colors, plotOutput("p")),
213+
#' function(input, output, session) {
214+
#' shinyOptions(plot.autotheme = TRUE)
215+
#' output$p <- renderPlot(p)
216+
#' }
217+
#' )
218+
#' }
199219
#'
200220
#' if (interactive()) {
201221
#' shinyApp(
202222
#' fluidPage(
203-
#' tags$style(HTML("body {background-color: black; color: white}")),
223+
#' base_colors,
224+
#' selectInput(
225+
#' "accent_color", "Select an accent color",
226+
#' colors(), selected = "darkred"
227+
#' ),
204228
#' plotOutput("p")
205229
#' ),
206-
#' function(input, output) {
207-
#' output$p <- renderPlot({
208-
#' ggplot(mtcars, aes(wt, mpg)) +
209-
#' geom_point(aes(color = mpg)) +
210-
#' geom_smooth()
211-
#' })
230+
#' function(input, output, session) {
231+
#' # Options could also be set globally via shinyOptions()
232+
#' opts <- autoThemeOptions(accent = reactive(input$accent_color))
233+
#' output$p <- renderPlot(p, autoTheme = opts)
212234
#' }
213235
#' )
214236
#' }
215237
#'
238+
#'
216239
autoThemeOptions <- function(bg = NULL, fg = NULL, accent = NULL,
217240
qualitative = NULL, sequential = NULL) {
218241
list(
@@ -364,6 +387,7 @@ getTheme <- function(autoTheme, session, outputName) {
364387
return(NULL)
365388
}
366389
autoTheme <- if (isTRUE(autoTheme)) list() else autoTheme
390+
autoTheme <- lapply(autoTheme, function(x) if (is.reactive(x)) x() else x)
367391
# default to computed styles from the client
368392
colors <- c("bg", "fg", "accent")
369393
for (col in colors) {

man/autoThemeOptions.Rd

Lines changed: 32 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)