Great question. First I want to say this behavior (i.e., an observer error ending the session) was a deliberate design choice. Without that behavior, you have no visual indication when an error happens. That being said, I can understand why you might want to have safe evaluation. Here's a function you could use to try to evaluate an expression within an observer, and if it fails, relay the error in a notification (thanks to @winston for recommending it)
library(shiny)
tryObserve <- function(x) {
x <- substitute(x)
env <- parent.frame()
observe({
tryCatch(
eval(x, env),
error = function(e) {
showNotification(paste("Error: ", e$message), type = "error")
}
)
})
}
shinyApp(
fluidPage(
actionButton("go", "Go")
),
function(input, output) {
tryObserve({
if (is.null(input$go) || input$go == 0)
return()
message("hi")
stop("oops")
message("bye")
})
}
)