Skip to contents

Wraps the UI and server functions of a module in a shiny app, to develop and test the module. Around the module UI, the app shows what goes into and comes out of the module server:

  • above, the server arguments (server_args),

  • below, the return values of the module server.

Usage

module2app_ui(
  module_ui = NULL,
  ui_args = list(),
  ui_wrapper = shiny::basicPage
)

module2app_server(module_server = NULL, server_args = list())

module2app(
  module_ui = NULL,
  module_server = NULL,
  ui_args = list(),
  server_args = list(),
  ui_wrapper = shiny::basicPage,
  options = list(test.mode = TRUE),
  ...
)

Arguments

module_ui, module_server

Module functions.

ui_args, server_args

Additional arguments passed to module_serverand module_ui. server_args can be shiny::reactive()s, if corresponding argument in module_server accepts it. Named reactive server_args can change while the app runs (see module2app_bare()).

ui_wrapper

A function to wrap the resulting shiny::tagList() in. Must yield a full shiny UI, such as shiny::basicPage() or shiny::bootstrapPage(). For maximum reusability of a module, avoid depending on the wrapper and only return "vanilla" UI.

options

Named options that should be passed to the runApp call (these can be any of the following: "port", "launch.browser", "host", "quiet", "display.mode" and "test.mode"). You can also specify width and height parameters which provide a hint to the embedding environment about the ideal height/width for the app.

...

Arguments passed on to shiny::shinyApp

ui

The UI definition of the app (for example, a call to fluidPage() with nested controls).

If bookmarking is enabled (see enableBookmarking), this must be a single argument function that returns the UI definition.

server

A function with three parameters: input, output, and session. The function is called once for each session ensuring that each app is independent.

onStart

A function that will be called before the app is actually run. This is only needed for shinyAppObj, since in the shinyAppDir case, a global.R file can be used for this purpose.

uiPattern

A regular expression that will be applied to each GET request to determine whether the ui should be used to handle the request. Note that the entire request path must match the regular expression in order for the match to be considered successful.

enableBookmarking

Can be one of "url", "server", or "disable". The default value, NULL, will respect the setting from any previous calls to enableBookmarking(). See enableBookmarking() for more information on bookmarking your app.

Details

It shows each as an unevaluated and an evaluated tree (see mixed_react_tree()), which collapse when you click their heading. Collapsed trees still update. You can pass reactive values as server arguments, to observe how your module reacts to them.

For the module UI alone, without these displays, use module2app_bare().

If you only want to test the server logic of a module, you don't need this function. You can just pass the module server function to shiny::testServer(). Hadley Wickhams Mastering Shiny as a section on testing modules.

Both testbeds export the server arguments and return values for tests, and accept changes to reactive server_args while the app runs (see module2app_bare()).

Screenshots from local_app_driver() show only your actual module UI, without the displays of server arguments and return values.

Functions

  • module2app_ui(): UI with the module UI between the displays of server arguments and return values. It passes ui_wrapper four arguments: the heading, the server arguments, the module UI and the return values. A ui_wrapper with four parameters can arrange them, such as in sidebars.

  • module2app_server(): Server with the module server and the displays of server arguments and return values.

Screenshots from Tests

name: double-counter, variant: linux Screenshot from App

Examples

# ui wrapper with a custom function
ui_wrapper <- function(...) {
  shiny::navbarPage(
    shiny::tabPanel(title = "A Panel", ...),
    title = "Testbed with Navbar"
  )
}
# you can provide your own ui wrapper with partialised arguments
if (elf::is_installed2("bslib")) {
  # current bootstrap theme via bslib
  theme <- bslib::bs_theme(version = 5)
  ui_wrapper <- purrr::partial(shiny::bootstrapPage, theme = theme)
}
# ui wrapper which arranges the parts of the testbed
if (elf::is_installed2("bslib")) {
  ui_wrapper <- function(heading, inputs, module, returns) {
    bslib::page_fluid(
      heading,
      bslib::layout_sidebar(
        sidebar = bslib::sidebar(inputs),
        bslib::layout_sidebar(
          sidebar = bslib::sidebar(returns, position = "right"),
          module
        )
      )
    )
  }
}