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