Last active
February 26, 2024 23:10
-
-
Save sckott/85be8bd850b8c9dd86b393eed9956459 to your computer and use it in GitHub Desktop.
safeError behavior in observeEvent vs. eventReactive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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