Skip to content

Instantly share code, notes, and snippets.

@sckott
Last active February 26, 2024 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sckott/85be8bd850b8c9dd86b393eed9956459 to your computer and use it in GitHub Desktop.
Save sckott/85be8bd850b8c9dd86b393eed9956459 to your computer and use it in GitHub Desktop.
safeError behavior in observeEvent vs. eventReactive
library(shiny)
library(bslib)
library(ggplot2)
ui <- page_sidebar(
title = h1(
class = "bslib-page-title",
"Errors when bins == 29"
),
sidebar = sidebar(
title = "Histogram controls",
numericInput("bins", "Number of bins", 30),
actionButton("submit_button", "Submit"),
actionButton("send_button", "Send")
),
card(
card_header("Histogram"),
plotOutput("p")
)
)
server <- function(input, output) {
make_plot <- function() {
ggplot(iris) +
geom_histogram(aes(Sepal.Length), bins = input$bins) +
theme_bw(base_size = 20)
}
observeEvent(input$submit_button, {
output$p <- renderPlot({
if (input$bins == 29) stop(safeError("Error in stuff"))
make_plot()
})
})
xx <- eventReactive(input$send_button, {
if (input$bins == 29) stop(safeError("Error in things"))
make_plot()
})
output$p <- renderPlot({ xx() })
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment