简体   繁体   English

使用辅助导航的 shiny.router 和 navbarPage 的 URI 路由

[英]URI routing with shiny.router and navbarPage using secondary navigation

I am trying to create an R shiny website with a plethora of pages (it is supposed to be based off of a textbook.) Note that I am not that experienced in web-development.我正在尝试创建一个包含大量页面的 R 闪亮网站(它应该基于教科书。)请注意,我在网络开发方面经验不足。 I am a statistics & math major, hence the use of R.我是统计和数学专业的,因此使用了 R。

I have decided to use R Shiny Router since it seems the best for my multi-page purpose.我决定使用R Shiny Router ,因为它似乎最适合我的多页用途。

However, I would really appreciate a second navigation bar.但是,我真的很感激第二个导航栏。 I saw this tutorial using navbarPage() and navbarMenu() that has a secondary navigation feature.我看到本教程使用具有辅助导航功能的 navbarPage() 和 navbarMenu()。 This would be awesome so I can easily divide each page by sections!这太棒了,这样我就可以轻松地将每一页分成几个部分!

Currently my router looks like the following:目前我的路由器如下所示:

library(shiny)
library(shiny.router)

router = make_router(
  route("/", div("home_page")),
  route("section_3.2.1", div("page_3.2.1")),
  route("section_3.2.2", div("page_3.2.2")),
  route("section_3.2.3", div("page_3.2.3")),
  route("section_3.2.4", div("page_3.2.4")),
  route("section_3.3.1", div("page_3.3.1")),
  route("section_3.3.2", div("page_3.3.2")),
  route("contact", div("contact_page"))
)

ui = fluidPage(
  theme = "main.css",
  tags$ul(
    tags$li(a(href = route_link("/"), "Home Page")),
    tags$li(a(href = route_link("section_3.2.1"), "Section 3.2.1")),
    tags$li(a(href = route_link("section_3.2.2"), "Section 3.2.2")),
    tags$li(a(href = route_link("section_3.2.3"), "Section 3.2.3")),
    tags$li(a(href = route_link("section_3.2.4"), "Section 3.2.4")),
    tags$li(a(href = route_link("section_3.3.1"), "Section 3.3.1")),
    tags$li(a(href = route_link("section_3.3.2"), "Section 3.3.2")),
    tags$li(a(href = route_link("contact"), "Contact Page"))
  ),
  router$ui
)

server <- function(input, output, session) {
  router$server(input, output, session)
}

shinyApp(ui, server)

I naively tried the following, hoping it would work (note I just tested it with one section, 3.2, for now)我天真地尝试了以下方法,希望它能起作用(注意我现在只用一个部分 3.2 测试了它)

ui = navbarPage("Website Title",
     theme = "main.css",
       tags$ul(
         tags$li(a(href = route_link("/"), "Home Page")),
         tags$li(a(href = route_link("contact"), "Contact Page"))
       ),
     navbarMenu("Section 3.2",
        tags$ul(
          tags$li(a(href = route_link("section_3.2.1"), "Section 3.2.1")),
          tags$li(a(href = route_link("section_3.2.2"), "Section 3.2.2")),
          tags$li(a(href = route_link("section_3.2.3"), "Section 3.2.3")),
          tags$li(a(href = route_link("section_3.2.4"), "Section 3.2.4")),
        )),
    router$ui
)

I obtain this error:我收到此错误:

Error: Navigation containers expect a collection of `bslib::nav()`/`shiny::tabPanel()`s and/or `bslib::nav_menu()`/`shiny::navbarMenu()`s. Consider using `header` or `footer` if you wish to place content above (or below) every panel's contents.
In addition: Warning messages:
1: Navigation containers expect a collection of `bslib::nav()`/`shiny::tabPanel()`s and/or `bslib::nav_menu()`/`shiny::navbarMenu()`s. Consider using `header` or `footer` if you wish to place content above (or below) every panel's contents.
2: Navigation containers expect a collection of `bslib::nav()`/`shiny::tabPanel()`s and/or `bslib::nav_menu()`/`shiny::navbarMenu()`s. Consider using `header` or `footer` if you wish to place content above (or below) every panel's contents.

To me, it seems like if I need to use navbarMenu() and/or navbarPage(), I must use tabPanel(), which I don't want to due to the length of my code.对我来说,如果我需要使用 navbarMenu() 和/或 navbarPage(),我似乎必须使用 tabPanel(),由于代码的长度,我不想使用它。

Please let me know if there's a method to have a secondary navigation bar that is compatible with R Shiny Router?请让我知道是否有一种方法可以使辅助导航栏与 R Shiny Router 兼容? Thanks to anyone who answers!感谢任何回答的人!

Edit: to make it more clear with what output I'm looking for exactly, I'm looking for the following:编辑:为了更清楚我正在寻找的输出,我正在寻找以下内容:

预期输出

Currently I just have every single page, ie, home, section 3.2.1, section 3.2.2, ..., section 3.3.2, contact, all on top of the page.目前我只有每一页,即主页、第 3.2.1 节、第 3.2.2 节、...、第 3.3.2 节、联系人,都在页面顶部。 This is messy and unsustainable because I need to add sections 4 and 5.这是混乱且不可持续的,因为我需要添加第 4 节和第 5 节。

It seems the routing approach library(shiny.router) uses is not suitable to synchronize it with the selections made on a navbarPage , as we can't use router$ui as a child of tabPanel multiple times.似乎路由方法library(shiny.router)使用不适合将其与在navbarPage上所做的选择同步,因为我们不能多次使用router$ui作为tabPanel的子级。

However, we can do the routing using basic shiny as already explained here :然而,我们可以使用 basic shiny 进行路由,正如这里已经解释的那样:

library(shiny)

ui <- navbarPage(title = "My Application",
                 tabPanel("Home", "Home content"),
                 navbarMenu("Section 3.2",
                            # To avoid the need to parse encoded URLs via utils::URLdecode use e.g.:
                            # tabPanel(title = "Section 3.2.1", "3.2.1 content", value = "section_3.2.1"),
                            tabPanel("Section 3.2.1", "3.2.1 content"),
                            tabPanel("Section 3.2.2", "3.2.2 content"),
                            tabPanel("Section 3.2.3", "3.2.3 content"),
                            tabPanel("Section 3.2.4", "3.2.4 content")
                 ),
                 navbarMenu("Section 3.3",
                            tabPanel("Section 3.3.1", "3.3.1 content"),
                            tabPanel("Section 3.3.2", "3.3.2 content")
                 ),
                 tabPanel("Contact", "Contact content"),
                 id = "navbarID",
                 theme = "main.css"
)

server <- function(input, output, session) {
  observeEvent(session$clientData$url_hash, {
    currentHash <- utils::URLdecode(sub("#", "", session$clientData$url_hash))
    if(is.null(input$navbarID) || !is.null(currentHash) && currentHash != input$navbarID){
      freezeReactiveValue(input, "navbarID")
      updateNavbarPage(session, "navbarID", selected = currentHash)
    }
  }, priority = 1)
  
  observeEvent(input$navbarID, {
    currentHash <- sub("#", "", session$clientData$url_hash)
    pushQueryString <- paste0("#", input$navbarID)
    if(is.null(currentHash) || currentHash != input$navbarID){
      freezeReactiveValue(input, "navbarID")
      updateQueryString(pushQueryString, mode = "push", session)
    }
  }, priority = 0)
}

shinyApp(ui, server)

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM