Session object — session
R/shiny.R
Description
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
).
Value
- allowReconnect(value)
If
value
isTRUE
and run in a hosting environment (Shiny Server or Connect) with reconnections enabled, then when the session ends due to the network connection closing, the client will attempt to reconnect to the server. If a reconnection is successful, the browser will send all the current input values to the new session on the server, and the server will recalculate any outputs and send them back to the client. Ifvalue
isFALSE
, reconnections will be disabled (this is the default state). If"force"
, then the client browser will always attempt to reconnect. The only reason to use"force"
is for testing on a local connection (without Shiny Server or Connect).- clientData
A
reactiveValues()
object that contains information about the client.pixelratio
reports the "device pixel ratio" from the web browser, or 1 if none is reported. The value is 2 for Apple Retina displays.singletons
- for internal useurl_protocol
,url_hostname
,url_port
,url_pathname
,url_search
,url_hash_initial
andurl_hash
can be used to get the components of the URL that was requested by the browser to load the Shiny app page. These values are from the browser's perspective, so neither HTTP proxies nor Shiny Server will affect these values. Theurl_search
value may be used withparseQueryString()
to access query string parameters.
clientData
also contains information about each output.output_outputId_width
andoutput_outputId_height
give the dimensions (usingoffsetWidth
andoffsetHeight
) of the DOM element that is bound tooutputId
, andoutput_outputId_hidden
is a logical that indicates whether the element is hidden. These values may beNULL
if the output is not bound.- input
The session's
input
object (the same as is passed into the Shiny server function as an argument).- isClosed()
A function that returns
TRUE
if the client has disconnected.- ns(id)
Server-side version of
ns <- NS(id)
. If bare IDs need to be explicitly namespaced for the current module,session$ns("name")
will return the fully-qualified ID.- onEnded(callback)
Synonym for
onSessionEnded
.- onFlush(func, once=TRUE)
Registers a function to be called before the next time (if
once=TRUE
) or every time (ifonce=FALSE
) Shiny flushes the reactive system. Returns a function that can be called with no arguments to cancel the registration.- onFlushed(func, once=TRUE)
Registers a function to be called after the next time (if
once=TRUE
) or every time (ifonce=FALSE
) Shiny flushes the reactive system. Returns a function that can be called with no arguments to cancel the registration.- 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
output
object (the same as is passed into the Shiny server function as an argument).- reactlog
For internal use.
- registerDataObj(name, data, filterFunc)
Publishes any R object as a URL endpoint that is unique to this session.
name
must be a single element character vector; it will be used to form part of the URL.filterFunc
must be a function that takes two arguments:data
(the value that was passed intoregisterDataObj
) andreq
(an environment that implements the Rook specification for HTTP requests).filterFunc
will be called with these values whenever an HTTP request is made to the URL endpoint. The return value offilterFunc
should be a Rook-style response.- 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
user
's relevant group information. Useful for determining what privileges the user should or shouldn't have.- resetBrush(brushId)
Resets/clears the brush with the given
brushId
, if it exists on anyimageOutput
orplotOutput
in the app.- sendCustomMessage(type, message)
Sends a custom message to the web page.
type
must be a single-element character vector giving the type of message, whilemessage
can be any jsonlite-encodable value. Custom messages have no meaning to Shiny itself; they are used solely to convey information to custom JavaScript logic in the browser. You can do this by adding JavaScript code to the browser that callsShiny.addCustomMessageHandler(type, function(message){...})
as the page loads; the function you provide toaddCustomMessageHandler
will be invoked each timesendCustomMessage
is called on the server.- sendBinaryMessage(type, message)
Similar to
sendCustomMessage
, but the message must be a raw vector and the registration method on the client isShiny.addBinaryMessageHandler(type, function(message){...})
. The message argument on the client will be a DataView.- 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
receiveMessage(el, message)
method will be called.sendInputMessage
should generally not be called directly from Shiny apps, but through friendlier wrapper functions likeupdateTextInput()
.- 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
shiny.testmode
option is set to TRUE. For the input, output, and export arguments, TRUE means to return all of these values. It is also possible to specify by name which values to return by providing a character vector, as ininput=c("x", "y")
. The format can be "rds" or "json".- setCurrentTheme(theme)
Sets the current
bootstrapLib()
theme, which updates the value ofgetCurrentTheme()
, invalidatessession$getCurrentTheme()
, and calls function(s) registered withregisterThemeDependency()
with providedtheme
. If those function calls returnhtmltools::htmlDependency()
s withstylesheet
s, then those stylesheets are "refreshed" (i.e., the new stylesheets are inserted on the page and the old ones are disabled and removed).- getCurrentTheme()
A reactive read of the current
bootstrapLib()
theme.