| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/shiny.R |
| 3 |
\name{onStop} |
| 4 |
\alias{onStop} |
| 5 |
\title{Run code after an application or session ends} |
| 6 |
\usage{ |
| 7 |
onStop(fun, session = getDefaultReactiveDomain()) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{fun}{A function that will be called after the app has finished running.} |
| 11 |
|
| 12 |
\item{session}{A scope for when the callback will run. If \code{onStop} is |
| 13 |
called from within the server function, this will default to the current |
| 14 |
session, and the callback will be invoked when the current session ends. If |
| 15 |
\code{onStop} is called outside a server function, then the callback will |
| 16 |
be invoked with the application exits. If \code{NULL}, it is the same as |
| 17 |
calling \code{onStop} outside of the server function, and the callback will |
| 18 |
be invoked when the application exits.} |
| 19 |
} |
| 20 |
\value{ |
| 21 |
A function which, if invoked, will cancel the callback. |
| 22 |
} |
| 23 |
\description{ |
| 24 |
This function registers callback functions that are invoked when the |
| 25 |
application exits (when \code{\link[=runApp]{runApp()}} exits), or after each user |
| 26 |
session ends (when a client disconnects). |
| 27 |
} |
| 28 |
\examples{ |
| 29 |
## Only run this example in interactive R sessions |
| 30 |
if (interactive()) { |
| 31 |
# Open this application in multiple browsers, then close the browsers. |
| 32 |
shinyApp( |
| 33 |
ui = basicPage("onStop demo"), |
| 34 |
|
| 35 |
server = function(input, output, session) { |
| 36 |
onStop(function() cat("Session stopped\n")) |
| 37 |
}, |
| 38 |
|
| 39 |
onStart = function() { |
| 40 |
cat("Doing application setup\n") |
| 41 |
|
| 42 |
onStop(function() { |
| 43 |
cat("Doing application cleanup\n") |
| 44 |
}) |
| 45 |
} |
| 46 |
) |
| 47 |
} |
| 48 |
# In the example above, onStop() is called inside of onStart(). This is |
| 49 |
# the pattern that should be used when creating a shinyApp() object from |
| 50 |
# a function, or at the console. If instead you are writing an app.R which |
| 51 |
# will be invoked with runApp(), you can do it that way, or put the onStop() |
| 52 |
# before the shinyApp() call, as shown below. |
| 53 |
|
| 54 |
\dontrun{ |
| 55 |
# ==== app.R ==== |
| 56 |
cat("Doing application setup\n") |
| 57 |
onStop(function() { |
| 58 |
cat("Doing application cleanup\n") |
| 59 |
}) |
| 60 |
|
| 61 |
shinyApp( |
| 62 |
ui = basicPage("onStop demo"), |
| 63 |
|
| 64 |
server = function(input, output, session) { |
| 65 |
onStop(function() cat("Session stopped\n")) |
| 66 |
} |
| 67 |
) |
| 68 |
# ==== end app.R ==== |
| 69 |
|
| 70 |
|
| 71 |
# Similarly, if you have a global.R, you can call onStop() from there. |
| 72 |
# ==== global.R ==== |
| 73 |
cat("Doing application setup\n") |
| 74 |
onStop(function() { |
| 75 |
cat("Doing application cleanup\n") |
| 76 |
}) |
| 77 |
# ==== end global.R ==== |
| 78 |
} |
| 79 |
} |
| 80 |
\seealso{ |
| 81 |
\code{\link[=onSessionEnded]{onSessionEnded()}} for the same functionality, but at |
| 82 |
the session level only. |
| 83 |
} |