| session | R Documentation |
Shiny server functions can optionally include session as a parameter
(e.g. function(input, output, session)). The session object is an
environment that can be used to access information and functionality
relating to the session. The following list describes the items available
in the environment; they can be accessed using the $ operator (for
example, session$clientData$url_search).
allowReconnect(value) |
If |
clientData |
A
|
input |
The session's |
isClosed() |
A function that returns |
ns(id) |
Server-side version of |
onDestroy(callback) |
Registers a callback to be invoked when the session scope ends.
Returns a function that can be called to unregister the callback.
For the root session, callbacks are invoked when the session closes,
after |
destroy(id = NULL) |
Destroys a module session scope (and any descendant scopes) by invoking
all registered Called with no removeUI(selector = "#editor")
session$destroy("editor")
On the root session an |
onEnded(callback) |
Synonym for |
onFlush(func, once=TRUE) |
Registers a function to be called before the next time (if |
onFlushed(func, once=TRUE) |
Registers a function to be called after the next time (if |
onSessionEnded(callback) |
Registers a function to be called after the client has disconnected. Returns a function that can be called with no arguments to cancel the registration. |
output |
The session's |
reactlog |
For internal use. |
registerDataObj(name, data, filterFunc) |
Publishes any R object as a URL endpoint that is unique to this session.
|
reload() |
The equivalent of hitting the browser's Reload button. Only works if the session is actually connected. |
request |
An environment that implements the Rook specification for HTTP requests. This is the request that was used to initiate the websocket connection (as opposed to the request that downloaded the web page for the app). |
userData |
An environment for app authors and module/package authors to store whatever session-specific data they want. |
user |
User's log-in information. Useful for identifying users on hosted platforms such as RStudio Connect and Shiny Server. |
groups |
The |
resetBrush(brushId) |
Resets/clears the brush with the given |
sendCustomMessage(type, message) |
Sends a custom message to the web page. |
sendBinaryMessage(type, message) |
Similar to |
sendInputMessage(inputId, message) |
Sends a message to an input on the session's client web page; if the input
is present and bound on the page at the time the message is received, then
the input binding object's |
setBookmarkExclude(names) |
Set input names to be excluded from bookmarking. |
getBookmarkExclude() |
Returns the set of input names to be excluded from bookmarking. |
onBookmark(fun) |
Registers a function that will be called just before bookmarking state. |
onBookmarked(fun) |
Registers a function that will be called just after bookmarking state. |
onRestore(fun) |
Registers a function that will be called when a session is restored, before all other reactives, observers, and render functions are run. |
onRestored(fun) |
Registers a function that will be called when a session is restored, after all other reactives, observers, and render functions are run. |
doBookmark() |
Do bookmarking and invoke the onBookmark and onBookmarked callback functions. |
exportTestValues() |
Registers expressions for export in test mode, available at the test snapshot URL. |
getTestSnapshotUrl(input=TRUE, output=TRUE, export=TRUE, format="json") |
Returns a URL for the test snapshots. Only has an effect when the
|
setCurrentTheme(theme) |
Sets the current |
getCurrentTheme() |
A reactive read of the current |
Every reactive object (values, expressions, observers) is scoped to the session in which it was created. Destroying a module session tears down only the objects in that scope; the parent session and sibling modules are unaffected. This scoping is what makes modules safe to add and remove dynamically — without it, destroying one module could leak callbacks or invalidate reactive objects that belong to another part of the app.
The parent that inserted the module's UI under an id can tear it down by
that same id, without the module having to hand anything back:
# In the parent server: insert, then later remove and destroy by id
observeEvent(input$add, {
insertUI("#container", ui = myModuleUI("editor"))
myModuleServer("editor")
})
observeEvent(input$remove, {
removeUI(selector = "#editor")
session$destroy("editor")
})
Inside the module, session$destroy() (with no id) destroys the module's
own scope. A module can expose this as a handle for callers that need to
trigger teardown from somewhere that doesn't have the parent session or
id (see the data ownership section below for an example).
The key rule: data that must outlive a module should live outside it. A reactive value created inside a module is destroyed with the module. A reactive value created in the caller's scope and passed in survives destruction.
Returning a reactive value from a module works when the module lives for
the entire session. However, if you plan to call session$destroy(), the
returned value will be destroyed and can no longer be read:
myModuleServer <- function(id) {
moduleServer(id, function(input, output, session) {
result <- reactiveVal(0)
# ... update result ...
list(result = result, destroy = session$destroy)
})
}
mod <- myModuleServer("editor")
mod$destroy()
mod$result()
#> Error: Can't access reactive; its module session has been destroyed
Instead, pass a reactive value into the module. The value lives in the caller's scope and survives destruction:
myModuleServer <- function(id, result) {
moduleServer(id, function(input, output, session) {
observeEvent(input$save, {
result(input$data)
})
})
}
# Caller creates and owns the value
saved_data <- reactiveVal(NULL)
myModuleServer("editor", result = saved_data)
# saved_data is still valid after the module is destroyed
moduleServer(), removeUI()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.