plot_with_settings: Plot-with-settings module

plot_with_settings_uiR Documentation

Plot-with-settings module

Description

[Stable] Universal module for plots with settings for height, width, and download.

Usage

plot_with_settings_ui(id)

plot_with_settings_srv(
  id,
  plot_r,
  height = c(600, 200, 2000),
  width = NULL,
  show_hide_signal = reactive(TRUE),
  brushing = FALSE,
  clicking = FALSE,
  dblclicking = FALSE,
  hovering = FALSE,
  graph_align = "left"
)

Arguments

id

(character(1)) shiny module id.

plot_r

(reactive or function)
reactive expression or a simple function to draw a plot. A simple function is needed e.g. for base plots like plot(1) as the output can not be caught when downloading. Take into account that simple functions are less efficient than reactive, as not catching the result.

height

(numeric, optional)
vector with three elements c(VAL, MIN, MAX), where VAL is the starting value of the slider in the main and modal plot display. The value in the modal display is taken from the value of the slider in the main plot display.

width

(numeric, optional)
vector with three elements c(VAL, MIN, MAX), where VAL is the starting value of the slider in the main and modal plot display; NULL for default display. The value in the modal display is taken from the value of the slider in the main plot display.

show_hide_signal

optional, (reactive logical a mechanism to allow modules which call this module to show/hide the plot_with_settings UI)

brushing

(logical, optional)
a mechanism to enable / disable brushing on the main plot (in particular: not the one displayed in modal). All the brushing data is stored as a reactive object in the "brush" element of returned list. See the example for details.

clicking

(logical)
a mechanism to enable / disable clicking on data points on the main plot (in particular: not the one displayed in modal). All the clicking data is stored as a reactive object in the "click" element of returned list. See the example for details.

dblclicking

(logical, optional)
a mechanism to enable / disable double-clicking on data points on the main plot (in particular: not the one displayed in modal). All the double clicking data is stored as a reactive object in the "dblclick" element of returned list. See the example for details.

hovering

(logical(1), optional)
a mechanism to enable / disable hovering over data points on the main plot (in particular: not the one displayed in modal). All the hovering data is stored as a reactive object in the "hover" element of returned list. See the example for details.

graph_align

(character(1), optional)
one of "left" (default), "center", "right" or "justify". The alignment of the graph on the main page.

Details

By default the plot is rendered with ⁠72 dpi⁠. In order to change this, to for example 96 set options(teal.plot_dpi = 96). The minimum allowed dpi value is 24 and it must be a whole number. If an invalid value is set then the default value is used and a warning is outputted to the console.

Value

A shiny module.

Examples

# Example using a reactive as input to plot_r
library(shiny)
app1 <- shinyApp(
  ui = fluidPage(
    plot_with_settings_ui(
      id = "plot_with_settings"
    )
  ),
  server = function(input, output, session) {
    plot_r <- reactive({
      ggplot2::ggplot(faithful, ggplot2::aes(x = waiting, y = eruptions)) +
        ggplot2::geom_point()
    })

    plot_with_settings_srv(
      id = "plot_with_settings",
      plot_r = plot_r,
      height = c(400, 100, 1200),
      width = c(500, 250, 750)
    )
  }
)

if (interactive()) {
  shinyApp(app1$ui, app1$server)
}

# Example using a function as input to plot_r
app2 <- shinyApp(
  ui = fluidPage(
    radioButtons("download_option", "Select the Option", list("ggplot", "trellis", "grob", "base")),
    plot_with_settings_ui(
      id = "plot_with_settings"
    ),
    sliderInput("nums", "Value", 1, 10, 1)
  ),
  server = function(input, output, session) {
    plot_r <- function() {
      numbers <- seq_len(input$nums)
      if (input$download_option == "ggplot") {
        ggplot2::ggplot(data.frame(n = numbers), ggplot2::aes(n)) +
          ggplot2::geom_bar()
      } else if (input$download_option == "trellis") {
        lattice::densityplot(numbers)
      } else if (input$download_option == "grob") {
        tr_plot <- lattice::densityplot(numbers)
        ggplot2::ggplotGrob(
          ggplot2::ggplot(data.frame(n = numbers), ggplot2::aes(n)) +
            ggplot2::geom_bar()
        )
      } else if (input$download_option == "base") {
        plot(numbers)
      }
    }

    plot_with_settings_srv(
      id = "plot_with_settings",
      plot_r = plot_r,
      height = c(400, 100, 1200),
      width = c(500, 250, 750)
    )
  }
)

if (interactive()) {
  shinyApp(app2$ui, app2$server)
}

# Example with brushing/hovering/clicking/double-clicking
app3 <- shinyApp(
  ui = fluidPage(
    plot_with_settings_ui(
      id = "plot_with_settings"
    ),
    fluidRow(
      column(4, h3("Brush"), verbatimTextOutput("brushing_data")),
      column(4, h3("Click"), verbatimTextOutput("clicking_data")),
      column(4, h3("DblClick"), verbatimTextOutput("dblclicking_data")),
      column(4, h3("Hover"), verbatimTextOutput("hovering_data"))
    )
  ),
  server = function(input, output, session) {
    plot_r <- reactive({
      ggplot2::ggplot(faithful, ggplot2::aes(x = waiting, y = eruptions)) +
        ggplot2::geom_point()
    })

    plot_data <- plot_with_settings_srv(
      id = "plot_with_settings",
      plot_r = plot_r,
      height = c(400, 100, 1200),
      brushing = TRUE,
      clicking = TRUE,
      dblclicking = TRUE,
      hovering = TRUE
    )

    output$brushing_data <- renderPrint(plot_data$brush())
    output$clicking_data <- renderPrint(plot_data$click())
    output$dblclicking_data <- renderPrint(plot_data$dblclick())
    output$hovering_data <- renderPrint(plot_data$hover())
  }
)

if (interactive()) {
  shinyApp(app3$ui, app3$server)
}

# Example which allows module to be hidden/shown
library("shinyjs")

app4 <- shinyApp(
  ui = fluidPage(
    useShinyjs(),
    actionButton("button", "Show/Hide"),
    plot_with_settings_ui(
      id = "plot_with_settings"
    )
  ),
  server = function(input, output, session) {
    plot_r <- plot_r <- reactive(
      ggplot2::ggplot(faithful, ggplot2::aes(x = waiting, y = eruptions)) +
        ggplot2::geom_point()
    )

    show_hide_signal_rv <- reactiveVal(TRUE)

    observeEvent(input$button, show_hide_signal_rv(!show_hide_signal_rv()))

    plot_with_settings_srv(
      id = "plot_with_settings",
      plot_r = plot_r,
      height = c(400, 100, 1200),
      width = c(500, 250, 750),
      show_hide_signal = reactive(show_hide_signal_rv())
    )
  }
)

if (interactive()) {
  shinyApp(app4$ui, app4$server)
}


teal.widgets documentation built on May 29, 2024, 2:41 a.m.