Title: | Switch Between Alternative 'shiny' UIs |
---|---|
Description: | Sometimes it is useful to serve up alternative 'shiny' UIs depending on information passed in the request object, such as the value of a cookie or a query parameter. This packages facilitates such switches. |
Authors: | Jon Harmon [aut, cre, cph] |
Maintainer: | Jon Harmon <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0.9000 |
Built: | 2024-11-07 20:17:16 UTC |
Source: | https://github.com/r4ds/scenes |
Specify a function that uses actions and the request object to choose which Shiny UI to serve.
change_scene(..., fall_through = default_ui)
change_scene(..., fall_through = default_ui)
... |
One or more |
fall_through |
A ui to display if no scenes are valid. The
default value, |
A function that processes the request object to deliver a Shiny ui.
scene1 <- set_scene( "A shiny ui", req_has_query("scene", 1) ) scene2 <- set_scene( "Another shiny ui", req_has_query("scene", 2) ) ui <- change_scene( scene1, scene2 ) ui
scene1 <- set_scene( "A shiny ui", req_has_query("scene", 1) ) scene2 <- set_scene( "Another shiny ui", req_has_query("scene", 2) ) ui <- change_scene( scene1, scene2 ) ui
Generate the check function for an action, and use it to create a
scene_action
object.
construct_action(fn, ..., negate = FALSE, methods = "GET")
construct_action(fn, ..., negate = FALSE, methods = "GET")
fn |
A function that takes a request (and potentially other arguments)
and returns |
... |
Additional parameters passed on to |
negate |
If |
methods |
The http methods which needs to be accepted in order for this function to make sense. Default "GET" should work in almost all cases. |
A scene_action
.
simple_function <- function(request) { !missing(request) && length(request) > 0 } sample_action <- construct_action(simple_function) sample_action$check_fn() sample_action$check_fn(list()) sample_action$check_fn(list(a = 1))
simple_function <- function(request) { !missing(request) && length(request) > 0 } sample_action <- construct_action(simple_function) sample_action$check_fn() sample_action$check_fn(list()) sample_action$check_fn(list(a = 1))
A plain text UI that returns an HTTP status of 422, indicating that the request was well-formed, but semantically incorrect.
default_ui()
default_ui()
A plain text UI with status code 422.
default_ui()
default_ui()
Create a scene_action
specifying a cookie that must
be present (or absent) and optionally a check function for that cookie.
req_has_cookie(cookie_name, validation_fn = NULL, ..., negate = FALSE)
req_has_cookie(cookie_name, validation_fn = NULL, ..., negate = FALSE)
cookie_name |
The cookie that must be present, as a length-1 character vector. |
validation_fn |
A function that takes the value of the cookie as the
first parameter, and returns |
... |
Additional parameters passed on to |
negate |
If |
A scene_action
, to be used in set_scene()
.
# Specify an action to detect a cookie named "mycookie". req_has_cookie("mycookie") # Specify an action to detect the *lack* of a cookie named "mycookie". req_has_cookie("mycookie", negate = TRUE) # Specify an action to detect a cookie named "mycookie" that has 27 # characters. req_has_cookie( cookie_name = "mycookie", validation_fn = function(cookie_value) { nchar(cookie_value == 27) } ) # Specify an action to detect a cookie named "mycookie" that has a # variable-defined number of characters. expect_n_chars <- function(x, N) { nchar(x) == N } my_N <- 27 # Perhaps set by an environment variable. req_has_cookie( cookie_name = "mycookie", validation_fn = expect_n_chars, N = my_N )
# Specify an action to detect a cookie named "mycookie". req_has_cookie("mycookie") # Specify an action to detect the *lack* of a cookie named "mycookie". req_has_cookie("mycookie", negate = TRUE) # Specify an action to detect a cookie named "mycookie" that has 27 # characters. req_has_cookie( cookie_name = "mycookie", validation_fn = function(cookie_value) { nchar(cookie_value == 27) } ) # Specify an action to detect a cookie named "mycookie" that has a # variable-defined number of characters. expect_n_chars <- function(x, N) { nchar(x) == N } my_N <- 27 # Perhaps set by an environment variable. req_has_cookie( cookie_name = "mycookie", validation_fn = expect_n_chars, N = my_N )
Create a scene_action
specifying a key that must be
present (or absent) in the query string (the part of the URL when the shiny
app was called, after "?"), and optionally a value or values for that key.
For example, in myapps.shinyapps.io/myapp?param1=1¶m2=text
,
?param1=1¶m2=text
is the query string, param1
and param2
are keys,
and 1
and text
are their corresponding values.
req_has_query(key, values = NULL, negate = FALSE)
req_has_query(key, values = NULL, negate = FALSE)
key |
The key that must be present, as a length-1 character vector. |
values |
Details about what to look for in the |
negate |
If |
A scene_action
, to be used in set_scene()
.
# Specify an action to detect a "code" parameter in the query. req_has_query("code") # Specify an action to detect the *lack* of a "code" parameter in the query. req_has_query("code", negate = TRUE) # Specify an action to detect a "language" parameter, with values containing # "en" or "es". req_has_query("language", "en|es")
# Specify an action to detect a "code" parameter in the query. req_has_query("code") # Specify an action to detect the *lack* of a "code" parameter in the query. req_has_query("code", negate = TRUE) # Specify an action to detect a "language" parameter, with values containing # "en" or "es". req_has_query("language", "en|es")
Create a scene_action
specifying the HTTP method that
must be used (or not used).
req_uses_method(method, negate = FALSE) req_uses_get(negate = FALSE) req_uses_post(negate = FALSE)
req_uses_method(method, negate = FALSE) req_uses_get(negate = FALSE) req_uses_post(negate = FALSE)
method |
The expected HTTP method. |
negate |
If |
A scene_action
, to be used in set_scene()
.
req_uses_method("GET") req_uses_method("POST") req_uses_get() req_uses_get(negate = TRUE) req_uses_post() req_uses_post(negate = TRUE)
req_uses_method("GET") req_uses_method("POST") req_uses_get() req_uses_get(negate = TRUE) req_uses_post() req_uses_post(negate = TRUE)
scene_action
classA scene_action
object is a list
with components check_fn
and methods
. It is used to test whether a request should trigger a
particlar scene.
Define a shiny_scene
by linking a UI to zero or more
scene_action
requirements.
set_scene(ui, ...)
set_scene(ui, ...)
ui |
A shiny ui. |
... |
Zero or more |
A shiny_scene
.
scene1 <- set_scene( "A shiny ui", req_has_query("scene", 1) ) scene1 scene2 <- set_scene( "Another shiny ui", req_has_query("scene", 2) ) scene2
scene1 <- set_scene( "A shiny ui", req_has_query("scene", 1) ) scene1 scene2 <- set_scene( "Another shiny ui", req_has_query("scene", 2) ) scene2
shiny_scene
classA shiny_scene
object is a list
with components ui
and
actions
. It is used to define what should display in a Shiny app in
different scenarios.