router: Create a router

View source: R/router.R

routerR Documentation

Create a router

Description

A router() defines a multi-page Shiny application using a set of router_page()s. See router_server() and router_ui() to create a multi-page application from the returned router object.

Usage

router(
  ...,
  callback_pageload = NULL,
  callback_default = NULL,
  page_403 = router_page("403", ui_403),
  page_404 = router_page("404", ui_404)
)

## S3 method for class 'router'
print(x, short = FALSE, ...)

Arguments

...

A set of router_page() objects. The first page is considered the default, and will be used if no path is provided.

callback_pageload

A function that is called immediately after page content is rendered. The function should take a router_page object and shiny session as input.

callback_default

A function that determines the default page. The function should take a shiny session as input and return the path of the default page. If not provided, the first page is used as the default.

page_403

A router_page to be used when a user attempts to access an unauthorised page.

page_404

A router_page to be used when a user attempts to access a page that doesn't exist.

x

A router object.

short

Should a one line summary be printed?

Value

A router object.

Callbacks

The callback function arguments allow user customisation of the page serving process.

  • callback_pageload is called after a new page is loaded. It should be a function with the signature function(page, session), taking a router_page and a shiny session as input. Thh return value of the function is ignored.

    Useful to perform operations based on page metadata.

    callback_pageload = function(page, session) {
      shinyjs::runjs(paste0("document.title = \"", page$title, "\";"))
    }
    
  • callback_default() is called to determine the path to serve when the default page is requested. It should be a function with the signature function(session), taking a shiny session as input and returning the path of the page to load.

    Useful for setting per-user or per-group home pages.

    callback_default = function(session) {
      if ("dev" %in% session$groups) {
        "devlanding"
      } else {
        "home"
      }
    }
    

HTTP response pages

router_pages are served in place of common HTTP response codes. These can be customised:

  • page_403 is served when the authorised function of a page returns FALSE.

  • page_404 is served when an unknown path is requested.

See Also

router_page() to create pages, add_router_callback() to add callback functions to an existing router, and router_server() and router_ui() to add a router to a Shiny application.

Examples

if (interactive()) {

ui_test <- function(id) {
  htmltools::p("Test!")
}

home_page <- router_page("home", ui_test)
other_page - router_page("other", ui_test)

test_router <- router(home_page, other_page)
}

gorcha/shinypages documentation built on June 29, 2022, 4:38 a.m.