tests/testthat/test-apps/HashRouter/App.R

library(shiny)

ui <- fluidPage(
  reactRouter::HashRouter(
    reactRouter::NavLink.shinyInput(
      inputId = "NavLinkHome",
      to = "/",
      "Home"
    ),
    div(),
    reactRouter::NavLink.shinyInput(
      inputId = "NavLinkPage",
      to = "page",
      "Page"
    ),
    hr(),
    reactRouter::Routes(
      reactRouter::Route(
        path = "/",
        element = div(
          uiOutput(outputId = "outputHome")
        )
      ),
      reactRouter::Route(
        path = "page",
        element = div(
          uiOutput(outputId = "outputPage")
        )
      )
    )
  )
)

server <- function(input, output, session) {
  # update session when clicking on specific navLink
  # session$reload() is the equivalent of hitting the browser's Reload button.
  observeEvent(input$NavLinkHome, {
    session$reload()
  })
  observeEvent(input$NavLinkPage, {
    session$reload()
  })
  output$outputHome <- renderUI({
    p("home content")
  })
  output$outputPage <- renderUI({
    p("page content")
  })
  # observe({
  #   # print full URL hash
  #   client_data <- reactiveValuesToList(session$clientData)
  #   url_hostname <- client_data$url_hostname
  #   url_port <- client_data$url_port
  #   url_hash <- client_data$url_hash
  #   print(paste0(url_hostname, ":", url_port, "/", url_hash))
  # })
}

shinyApp(ui, server)

Try the reactRouter package in your browser

Any scripts or data that you put into this service are public.

reactRouter documentation built on June 8, 2025, 11:47 a.m.