Skip to contents

The goal of reactRouter is to provide a wrapper around React Router (v6).

[!CAUTION] Breaking Change in v.0.1.2 : reloadDocument is now FALSE by default in NavLink() and Link(). Set reloadDocument = TRUE only when target routes contain Shiny server-rendered output like uiOutput() / renderUI().

Usage

You can easily add URL pages in a Quarto document or R Shiny app like so:

library(reactRouter)

RouterProvider(
  Route(
    path = "/",
    element = div(
      NavLink(to = "/", "Main"),
      NavLink(to = "/analysis", "Analysis"),
      Outlet()
    ),
    Route(index = TRUE, element = "Main content"),
    Route(path = "analysis", element = "Analysis content")
  )
)

Install

#remotes::install_github("lgnbhl/reactRouter") # development version

install.packages("reactRouter")

Example

Get started with a showcase example:

# print all examples available: reactRouterExample()
reactRouterExample("dynamic-segment")

Read the vignette here for detailed use cases with Quarto and R Shiny.

Choosing a router

The simplest way to add routing is RouterProvider(), which defaults to hash-based routing and works in Shiny apps, static HTML, and Quarto documents:

RouterProvider(
  Route(path = "/", element = ..., Route(...))
)
# type = "hash" (default) — URL shows /#/about, bookmarkable
# type = "memory"         — URL never changes, great for embedded widgets

Lower-level functions (createHashRouter(), createMemoryRouter(), HashRouter(), MemoryRouter(), BrowserRouter()) are also available for more control. See the Choosing a router vignette for details and code examples.

Contribute

Would you like to contribute to the package? Have a look at the current roadmap.