Debian Codegraph @

man/onFlush.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/shiny.R
3 \name{onFlush}
4 \alias{onFlush}
5 \alias{onFlushed}
6 \alias{onSessionEnded}
7 \alias{onUnhandledError}
8 \title{Add callbacks for Shiny session events}
9 \usage{
10 onFlush(fun, once = TRUE, session = getDefaultReactiveDomain())
11
12 onFlushed(fun, once = TRUE, session = getDefaultReactiveDomain())
13
14 onSessionEnded(fun, session = getDefaultReactiveDomain())
15
16 onUnhandledError(fun, session = getDefaultReactiveDomain())
17 }
18 \arguments{
19 \item{fun}{A callback function.}
20
21 \item{once}{Should the function be run once, and then cleared, or should it
22 re-run each time the event occurs. (Only for \code{onFlush} and
23 \code{onFlushed}.)}
24
25 \item{session}{A shiny session object.}
26 }
27 \description{
28 These functions are for registering callbacks on Shiny session events.
29 \code{onFlush} registers a function that will be called before Shiny flushes the
30 reactive system. \code{onFlushed} registers a function that will be called after
31 Shiny flushes the reactive system. \code{onUnhandledError} registers a function to
32 be called when an unhandled error occurs before the session is closed.
33 \code{onSessionEnded} registers a function to be called after the client has
34 disconnected.
35
36 These functions should be called within the application's server function.
37
38 All of these functions return a function which can be called with no
39 arguments to cancel the registration.
40 }
41 \section{Unhandled Errors}{
42
43 Unhandled errors are errors that aren't otherwise handled by Shiny or by the
44 application logic. In other words, they are errors that will either cause the
45 application to crash or will result in "Error" output in the UI.
46
47 You can use \code{onUnhandledError()} to register a function that will be called
48 when an unhandled error occurs. This function will be called with the error
49 object as its first argument. If the error is fatal and will result in the
50 session closing, the error condition will have the \code{shiny.error.fatal} class.
51
52 Note that the \code{onUnhandledError()} callbacks cannot be used to prevent the
53 app from closing or to modify the error condition. Instead, they are intended
54 to give you an opportunity to log the error or perform other cleanup
55 operations.
56 }
57
58 \examples{
59 \dontshow{if (interactive()) withAutoprint(\{ # examplesIf}
60 library(shiny)
61
62 ui <- fixedPage(
63 markdown(c(
64 "Set the number to 8 or higher to cause an error",
65 "in the `renderText()` output."
66 )),
67 sliderInput("number", "Number", 0, 10, 4),
68 textOutput("text"),
69 hr(),
70 markdown(c(
71 "Click the button below to crash the app with an unhandled error",
72 "in an `observe()` block."
73 )),
74 actionButton("crash", "Crash the app!")
75 )
76
77 log_event <- function(level, ...) {
78 ts <- strftime(Sys.time(), " [\%F \%T] ")
79 message(level, ts, ...)
80 }
81
82 server <- function(input, output, session) {
83 log_event("INFO", "Session started")
84
85 onUnhandledError(function(err) {
86 # log the unhandled error
87 level <- if (inherits(err, "shiny.error.fatal")) "FATAL" else "ERROR"
88 log_event(level, conditionMessage(err))
89 })
90
91 onStop(function() {
92 log_event("INFO", "Session ended")
93 })
94
95 observeEvent(input$crash, stop("Oops, an unhandled error happened!"))
96
97 output$text <- renderText({
98 if (input$number > 7) {
99 stop("that's too high!")
100 }
101 sprintf("You picked number \%d.", input$number)
102 })
103 }
104
105 shinyApp(ui, server)
106 \dontshow{\}) # examplesIf}
107 }
108 \seealso{
109 \code{\link[=onStop]{onStop()}} for registering callbacks that will be
110 invoked when the application exits, or when a session ends.
111 }