| Title: | Basic Routing for Shiny Web Applications |
|---|---|
| Description: | It is a simple router for your Shiny apps. The router allows you to create dynamic web applications with real-time User Interface and easily share url to pages within your Shiny apps. |
| Authors: | Ryszard Szymański [cre, aut], Jakub Nowicki [aut], Filip Stachura [aut], Dominik Krzemiński [aut], Krystian Igras [aut], Servet Ahmet Çizmeli [ctb], Appsilon Sp. z o.o. [cph] |
| Maintainer: | Ryszard Szymański <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.1 |
| Built: | 2026-05-21 09:33:22 UTC |
| Source: | https://github.com/appsilon/shiny.router |
Works by sending a message up to
our reactive input binding on the client side, which tells page.js to update
the window URL accordingly, then tells client side shiny that our reactive
input binding has changed, then that comes back down to our router callback
function and all other observers watching get_page() or similar.
change_page(page, session = shiny::getDefaultReactiveDomain(), mode = "push")change_page(page, session = shiny::getDefaultReactiveDomain(), mode = "push")
page |
The new URL to go to. Should just be the path component of the URL, with optional query, e.g. "/learner?id=%d" |
session |
The current Shiny session. |
mode |
("replace" or "push") whether to replace current history or push a new one.
More in |
This function dynamically removes bootstrap dependency when user opens specified bookmark. It should be inserted in head of bootstrap page.
disable_bootstrap_on_bookmark(bookmark)disable_bootstrap_on_bookmark(bookmark)
bookmark |
Bookmark name on which bootstrap dependency should be suppressed. |
This corresponds to what might be called the "path" component of a URL, except that we're using URLs with hashes before the path & query (e.g.: http://www.example.com/#!/virtual/path?and=params)
get_page(session = shiny::getDefaultReactiveDomain())get_page(session = shiny::getDefaultReactiveDomain())
session |
The current Shiny Session |
The current page in a length-1 character vector, or FALSE if the input has no value.
Convenience function to retrieve any params that were part of the requested page. The param values returned come from "httr::parse_url()"
get_query_param(field = NULL, session = shiny::getDefaultReactiveDomain())get_query_param(field = NULL, session = shiny::getDefaultReactiveDomain())
field |
If provided, retrieve only a param with this name. (Otherwise, return all params) |
session |
The Shiny session |
The full list of params on the URL (if any), as a list. Or, the single requested param (if present). Or NULL if there's no input, or no params.
Tell the reactive chain to halt if we're not on the specified page. Useful for making sure we don't waste cycles re-rendering the UI for pages that are not currently displayed.
is_page(page, session = shiny::getDefaultReactiveDomain(), ...)is_page(page, session = shiny::getDefaultReactiveDomain(), ...)
page |
The page to display. Should match one of the paths sent to the |
session |
Shiny session |
... |
Other parameters are sent through to |
Returned callback needs to be called within Shiny server code.
make_router(default, ..., page_404 = page404())make_router(default, ..., page_404 = page404())
default |
Main route to which all invalid routes should redirect. |
... |
All other routes defined with shiny.router::route function. |
page_404 |
Styling of page when wrong bookmark is open. See page404. |
Shiny router callback that should be run in server code with Shiny input and output lists.
## Not run: router <- make_router( route("/", root_page), route("/other", other_page), page_404 = page404( message404 = "Please check if you passed correct bookmark name!") ) ## End(Not run)## Not run: router <- make_router( route("/", root_page), route("/other", other_page), page_404 = page404( message404 = "Please check if you passed correct bookmark name!") ) ## End(Not run)
This is default 404 page.
PAGE_404_ROUTEPAGE_404_ROUTE
An object of class character of length 1.
The page which appear when path is wrong.
page404(page = NULL, message404 = NULL)page404(page = NULL, message404 = NULL)
page |
shiny page style, e.g. |
message404 |
message to display at the 404 website |
page404() # shiny::tags$div(h1("Not found")) page404(message404 = "ABC") # shiny::tags$div(h1("ABC"))page404() # shiny::tags$div(h1("Not found")) page404(message404 = "ABC") # shiny::tags$div(h1("ABC"))
Extract info about url path and parameters that follow ? sign.
parse_url_path(url_path)parse_url_path(url_path)
url_path |
character with link url |
parse_url_path allows parsing parameters lists from url. See more in examples.
Note that having query string appear before #! may cause browser to refresh
and thus reset Shiny session.
list containing two objects:
path
query, a list
parse_url_path("?a=1&b=foo") parse_url_path("?a=1&b[1]=foo&b[2]=bar/#!/") parse_url_path("?a=1&b[1]=foo&b[2]=bar/#!/other_page") parse_url_path("www.foo.bar/#!/other_page") parse_url_path("www.foo.bar?a=1&b[1]=foo&b[2]=bar/#!/other") parse_url_path("#!/?a=1&b[1]=foo&b[2]=bar") parse_url_path("#!/other_page?a=1&b[1]=foo&b[2]=bar") parse_url_path("www.foo.bar/#!/other?a=1&b[1]=foo&b[2]=bar")parse_url_path("?a=1&b=foo") parse_url_path("?a=1&b[1]=foo&b[2]=bar/#!/") parse_url_path("?a=1&b[1]=foo&b[2]=bar/#!/other_page") parse_url_path("www.foo.bar/#!/other_page") parse_url_path("www.foo.bar?a=1&b[1]=foo&b[2]=bar/#!/other") parse_url_path("#!/?a=1&b[1]=foo&b[2]=bar") parse_url_path("#!/other_page?a=1&b[1]=foo&b[2]=bar") parse_url_path("www.foo.bar/#!/other?a=1&b[1]=foo&b[2]=bar")
Create single route configuration.
route(path, ui, server = NA)route(path, ui, server = NA)
path |
Website route. |
ui |
Valid Shiny user interface. |
server |
Function that is called as callback on server side [deprecated] |
A route configuration.
## Not run: route("/", shiny::tags$div(shiny::tags$span("Hello world"))) route("main", shiny::tags$div(h1("Main page"), p("Lorem ipsum."))) ## End(Not run)## Not run: route("/", shiny::tags$div(shiny::tags$span("Hello world"))) route("main", shiny::tags$div(h1("Main page"), p("Lorem ipsum."))) ## End(Not run)
Adds /#!/ prefix to link.
route_link(path)route_link(path)
path |
character with path |
route link
route_link("abc") # /#!/abcroute_link("abc") # /#!/abc
Server part of the router.
router_server(root_page = "/", env = parent.frame())router_server(root_page = "/", env = parent.frame())
root_page |
Main page path. |
env |
Environment (only for advanced usage). |
Router pages server callback.
## Not run: server <- function(input, output, session) { router_server(root_page = "/") } ## End(Not run)## Not run: server <- function(input, output, session) { router_server(root_page = "/") } ## End(Not run)
Creates router UI in Shiny applications.
router_ui(default, ..., page_404 = page404(), env = parent.frame())router_ui(default, ..., page_404 = page404(), env = parent.frame())
default |
Main route to which all invalid routes should redirect. |
... |
All other routes defined with shiny.router::route function.
It's possible to pass routes in dynamic way with dynamic dots.
See |
page_404 |
Styling of page when invalid route is open. See page404. |
env |
Environment (only for advanced usage), makes it possible to use shiny.router inside shiny modules. |
If you are defining the router inside a shiny module,
we assume that the namespacing function defined in the UI is named as ns.
Application UI wrapped in a router.
## Not run: ui <- function() { router_ui( route("/", root_page(id = "root")), route("other", other_page(id = "other")), page_404 = page404( message404 = "Please check if you passed correct bookmark name!") ) } ## End(Not run) ## Not run: # create the list of routes dynamic_routes <- list( route("other2", other_page(id = "other2")), route("other3", other_page(id = "other3")) ) ui <- function() { router_ui( route("/", root_page(id = "root")), route("other", other_page(id = "other")), # then it's possible to inject a list of arguments into a function call using rlang::`!!!` !!!dynamic_routes, page_404 = page404( message404 = "Please check if you passed correct bookmark name!") ) } ## End(Not run)## Not run: ui <- function() { router_ui( route("/", root_page(id = "root")), route("other", other_page(id = "other")), page_404 = page404( message404 = "Please check if you passed correct bookmark name!") ) } ## End(Not run) ## Not run: # create the list of routes dynamic_routes <- list( route("other2", other_page(id = "other2")), route("other3", other_page(id = "other3")) ) ui <- function() { router_ui( route("/", root_page(id = "root")), route("other", other_page(id = "other")), # then it's possible to inject a list of arguments into a function call using rlang::`!!!` !!!dynamic_routes, page_404 = page404( message404 = "Please check if you passed correct bookmark name!") ) } ## End(Not run)