safe_ggplot: Safely render a 'ggplot' in Shiny application

View source: R/safe_ggplot.R

safe_ggplotR Documentation

Safely render a ggplot in Shiny application

Description

Safely render a ggplot in Shiny application

Usage

safe_ggplot(expr, data = NULL, session = shiny::getDefaultReactiveDomain())

Arguments

expr

Code to produce a ggplot object.

data

Argument passed to eval_tidy to evaluate expression.

session

Session object to send notification to.

Value

Output of ggplot_build.

Examples

if (interactive()) {
  library(shiny)
  library(ggplot2)

  ui <- fluidPage(
    fluidRow(
      column(
        width = 3,
        selectInput(
          inputId = "var",
          label = "Var:",
          choices = c("Sepal.Width", "Do.Not.Exist")
        )
      ),
      column(
        width = 9,
        plotOutput(outputId = "plot")
      )
    )
  )

  server <- function(input, output, session) {

    output$plot <- renderPlot({
      p <- ggplot(iris) +
        geom_point(aes_string("Sepal.Length", input$var))
      safe_ggplot(p)
    })

  }

  shinyApp(ui, server)
}

esquisse documentation built on Sept. 1, 2022, 5:05 p.m.