Learn R Programming

shiny (version 1.9.1)

MockShinySession: Mock Shiny Session

Description

An R6 class suitable for testing purposes. Simulates, to the extent possible, the behavior of the ShinySession class. The session parameter provided to Shiny server functions and modules is an instance of a ShinySession in normal operation.

Most kinds of module and server testing do not require this class be instantiated manually. See instead testServer().

In order to support advanced usage, instances of MockShinySession are unlocked so that public methods and fields of instances may be modified. For example, in order to test authentication workflows, the user or groups fields may be overridden. Modified instances of MockShinySession may then be passed explicitly as the session argument of testServer().

Arguments

Public fields

env

The environment associated with the session.

returned

The value returned by the module under test.

singletons

Hardcoded as empty. Needed for rendering HTML (i.e. renderUI).

clientData

Mock client data that always returns a size for plots.

output

The shinyoutputs associated with the session.

input

The reactive inputs associated with the session.

userData

An environment initialized as empty.

progressStack

A stack of progress objects.

token

On a real ShinySession, used to identify this instance in URLs.

cache

The session cache object.

appcache

The app cache object.

restoreContext

Part of bookmarking support in a real ShinySession but always NULL for a MockShinySession.

groups

Character vector of groups associated with an authenticated user. Always NULL for a MockShinySesion.

user

The username of an authenticated user. Always NULL for a MockShinySession.

options

A list containing session-level shinyOptions.

Active bindings

files

For internal use only.

downloads

For internal use only.

closed

Deprecated in ShinySession and signals an error.

session

Deprecated in ShinySession and signals an error.

request

An empty environment where the request should be. The request isn't meaningfully mocked currently.

Methods


Method new()

Create a new MockShinySession.

Usage

MockShinySession$new()


Method onFlush()

Define a callback to be invoked before a reactive flush

Usage

MockShinySession$onFlush(fun, once = TRUE)

Arguments

fun

The function to invoke

once

If TRUE, will only run once. Otherwise, will run every time reactives are flushed.


Method onFlushed()

Define a callback to be invoked after a reactive flush

Usage

MockShinySession$onFlushed(fun, once = TRUE)

Arguments

fun

The function to invoke

once

If TRUE, will only run once. Otherwise, will run every time reactives are flushed.


Method onEnded()

Define a callback to be invoked when the session ends

Usage

MockShinySession$onEnded(sessionEndedCallback)

Arguments

sessionEndedCallback

The callback to invoke when the session has ended.


Method isEnded()

Returns FALSE if the session has not yet been closed

Usage

MockShinySession$isEnded()


Method isClosed()

Returns FALSE if the session has not yet been closed

Usage

MockShinySession$isClosed()


Method close()

Closes the session

Usage

MockShinySession$close()


Method cycleStartAction()

Unsophisticated mock implementation that merely invokes

Usage

MockShinySession$cycleStartAction(callback)

Arguments

callback

The callback to be invoked.


Method fileUrl()

Base64-encode the given file. Needed for image rendering.

Usage

MockShinySession$fileUrl(name, file, contentType = "application/octet-stream")

Arguments

name

Not used

file

The file to be encoded

contentType

The content type of the base64-encoded string


Method setInputs()

Sets reactive values associated with the session$inputs object and flushes the reactives.

Usage

MockShinySession$setInputs(...)

Arguments

...

The inputs to set. These arguments are processed with rlang::list2() and so are dynamic. Input names may not be duplicated.

Examples

\dontrun{
session$setInputs(x=1, y=2)
}


Method .scheduleTask()

An internal method which shouldn't be used by others. Schedules callback for execution after some number of millis milliseconds.

Usage

MockShinySession$.scheduleTask(millis, callback)

Arguments

millis

The number of milliseconds on which to schedule a callback

callback

The function to schedule.


Method elapse()

Simulate the passing of time by the given number of milliseconds.

Usage

MockShinySession$elapse(millis)

Arguments

millis

The number of milliseconds to advance time.


Method .now()

An internal method which shouldn't be used by others.

Usage

MockShinySession$.now()

Returns

Elapsed time in milliseconds.


Method defineOutput()

An internal method which shouldn't be used by others. Defines an output in a way that sets private$currentOutputName appropriately.

Usage

MockShinySession$defineOutput(name, func, label)

Arguments

name

The name of the output.

func

The render definition.

label

Not used.


Method getOutput()

An internal method which shouldn't be used by others. Forces evaluation of any reactive dependencies of the output function.

Usage

MockShinySession$getOutput(name)

Arguments

name

The name of the output.

Returns

The return value of the function responsible for rendering the output.


Method ns()

Returns the given id prefixed by this namespace's id.

Usage

MockShinySession$ns(id)

Arguments

id

The id to prefix with a namespace id.

Returns

The id with a namespace prefix.


Method flushReact()

Trigger a reactive flush right now.

Usage

MockShinySession$flushReact()


Method makeScope()

Create and return a namespace-specific session proxy.

Usage

MockShinySession$makeScope(namespace)

Arguments

namespace

Character vector indicating a namespace.

Returns

A new session proxy.


Method setEnv()

Set the environment associated with a testServer() call, but only if it has not previously been set. This ensures that only the environment of the outermost module under test is the one retained. In other words, the first assignment wins.

Usage

MockShinySession$setEnv(env)

Arguments

env

The environment to retain.

Returns

The provided env.


Method setReturned()

Set the value returned by the module call and proactively flush. Note that this method may be called multiple times if modules are nested. The last assignment, corresponding to an invocation of setReturned() in the outermost module, wins.

Usage

MockShinySession$setReturned(value)

Arguments

value

The value returned from the module

Returns

The provided value.


Method getReturned()

Get the value returned by the module call.

Usage

MockShinySession$getReturned()

Returns

The value returned by the module call


Method genId()

Generate a distinct character identifier for use as a proxy namespace.

Usage

MockShinySession$genId()

Returns

A character identifier unique to the current session.


Method rootScope()

Provides a way to access the root MockShinySession from any descendant proxy.

Usage

MockShinySession$rootScope()

Returns

The root MockShinySession.


Method onUnhandledError()

Add an unhandled error callback.

Usage

MockShinySession$onUnhandledError(callback)

Arguments

callback

The callback to add, which should accept an error object as its first argument.

Returns

A deregistration function.


Method unhandledError()

Called by observers when a reactive expression errors.

Usage

MockShinySession$unhandledError(e, close = TRUE)

Arguments

e

An error object.

close

If TRUE, the session will be closed after the error is handled, defaults to FALSE.


Method freezeValue()

Freeze a value until the flush cycle completes.

Usage

MockShinySession$freezeValue(x, name)

Arguments

x

A ReactiveValues object.

name

The name of a reactive value within x.


Method onSessionEnded()

Registers the given callback to be invoked when the session is closed (i.e. the connection to the client has been severed). The return value is a function which unregisters the callback. If multiple callbacks are registered, the order in which they are invoked is not guaranteed.

Usage

MockShinySession$onSessionEnded(sessionEndedCallback)

Arguments

sessionEndedCallback

Function to call when the session ends.


Method registerDownload()

Associated a downloadable file with the session.

Usage

MockShinySession$registerDownload(name, filename, contentType, content)

Arguments

name

The un-namespaced output name to associate with the downloadable file.

filename

A string or function designating the name of the file.

contentType

A string of the content type of the file. Not used by MockShinySession.

content

A function that takes a single argument file that is a file path (string) of a nonexistent temp file, and writes the content to that file path. (Reactive values and functions may be used from this function.)


Method getCurrentOutputInfo()

Get information about the output that is currently being executed.

Usage

MockShinySession$getCurrentOutputInfo()

Returns

A list with with the name of the output. If no output is currently being executed, this will return NULL. output, or NULL if no output is currently executing.


Method clone()

The objects of this class are cloneable with this method.

Usage

MockShinySession$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

Run this code

## ------------------------------------------------
## Method `MockShinySession$setInputs`
## ------------------------------------------------

if (FALSE) {
session$setInputs(x=1, y=2)
}

Run the code above in your browser using DataLab