【问题标题】:Add link panel tabs in Shiny with various top level navigation bars使用各种顶级导航栏在 Shiny 中添加链接面板选项卡
【发布时间】:2015-09-15 23:18:55
【问题描述】:

我正在尝试在 Shiny 应用程序中使用导航栏创建链接。我在A gist of programatically changing panel tabs in Shiny 找到了以下代码。它适用于第一个导航面板,但对于第二个它不起作用。这是我的ui.R

require(shiny)


shinyUI(navbarPage("",
                   tabPanel("Foo",
                            tabsetPanel(
                              tabPanel("Foo1",                            
                                       sidebarLayout(
                                         sidebarPanel(
                                           tags$div(div(id="Foo2", tags$a("Foo2"))
                                           )
                                             ),
                                         mainPanel(p("hello Foo1"))
                              )),
                              tabPanel("Foo2",
                                       sidebarLayout(
                                         sidebarPanel(),
                                         mainPanel(p("hello Foo2"))), 
                                         HTML("<script>$('#Foo2').click(function() {
                                                        tabs = $('.tabbable .nav.nav-tabs li')
                                                         tabs.each(function() {
                                                            $(this).removeClass('active')
                                                         })
                                                         $(tabs[1]).addClass('active')

                                                         tabsContents = $('.tabbable .tab-content .tab-pane')
                                                         tabsContents.each(function() {
                                                            $(this).removeClass('active')
                                                         })
                                                         $(tabsContents[1]).addClass('active')

                                                        $('#Foo2').trigger('change').trigger('shown');

                                                     })</script>")
                                       )
                              )
                                ),
                   tabPanel("Bar",
                            tabsetPanel(
                              tabPanel("Bar1",                            
                                       sidebarLayout(
                                         sidebarPanel(
                                           tags$div(div(id="Bar2", tags$a("Bar2"))
                                           )
                                         ),
                                         mainPanel(p("hello Bar1"))
                                       )),
                              tabPanel("Bar2",
                                       sidebarLayout(
                                         sidebarPanel(),
                                         mainPanel(p("hello Bar2"))), 
                                       HTML("<script>$('#Bar2').click(function() {
                                                        tabs = $('.tabbable .nav.nav-tabs li')
                                                         tabs.each(function() {
                                                            $(this).removeClass('active')
                                                         })
                                                         $(tabs[1]).addClass('active')

                                                         tabsContents = $('.tabbable .tab-content .tab-pane')
                                                         tabsContents.each(function() {
                                                            $(this).removeClass('active')
                                                         })
                                                         $(tabsContents[1]).addClass('active')

                                                        $('#Bar2').trigger('change').trigger('shown');

                                                     })</script>")
                              )
                            )
                   )
                   )
)

当您单击 foo1 的侧边栏面板时,指向 foo2 的链接会响应。但是,当您在 Bar 中执行相同操作时,它会转到其他地方。

【问题讨论】:

    标签: java r shiny rstudio


    【解决方案1】:

    这就是解决方案;)。

    require(shiny)
    shinyUI(navbarPage("",
                tabPanel("Foo",
                   tabsetPanel(
                        tabPanel("Foo1",                            
                                  sidebarLayout(
                                      sidebarPanel(tags$div(div(id="Foo2", tags$a("Foo2")))),
                                      mainPanel(p("hello Foo1")))
                         ),
                         tabPanel("Foo2",
                                   sidebarLayout(
                                       sidebarPanel(),
                                       mainPanel(p("hello Foo2"))), 
                                       HTML("<script>$('#Foo2').click(function() {
                                            tabs = $('.tabbable .nav.nav-tabs li')
                                            tabs.each(function() {
                                            $(this).removeClass('active')
                                            })
                                            $(tabs[1]).addClass('active')
    
                                            tabsContents = $('.tabbable .tab-content .tab-pane')
                                            tabsContents.each(function() {
                                            $(this).removeClass('active')
                                            })
                                            $(tabsContents[1]).addClass('active')
    
                                            $('#Foo2').trigger('change').trigger('shown');
    
                                            })</script>")
                                       )
                              )
                            ),
                tabPanel("Bar",
                   tabsetPanel(
                         tabPanel("Bar1",                            
                                   sidebarLayout(
                                       sidebarPanel(tags$div(div(id="Bar2",tags$a("Bar2")))),
                                   mainPanel(p("hello Bar1"))
                                       )),
                         tabPanel("Bar2",
                                   sidebarLayout(
                                       sidebarPanel(),
                                       mainPanel(p("hello Bar2"))), 
                                       HTML("<script>$('#Bar2').click(function() {
                                            tabs = $('.tabbable .nav.nav-tabs li')
                                            tabs.each(function() {
                                            $(this).removeClass('active')
                                            })
                                            $(tabs[3]).addClass('active')
    
                                            tabsContents = $('.tabbable .tab-content .tab-pane')
                                            tabsContents.each(function() {
                                            $(this).removeClass('active')
                                            })
                                            $(tabsContents[3]).addClass('active')
    
                                            $('#Bar2').trigger('change').trigger('shown');
    
                                            })</script>")
    
    
    
    
    
    
    
    
    
    
                              )
                              )
                            )
                            )
                              )
    

    【讨论】:

    • 太棒了!感谢您的解决方案,我想我做错了什么。我指向了错误的标签!
    猜你喜欢
    • 2017-03-17
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 2022-01-19
    • 2021-06-02
    • 2017-11-03
    相关资源
    最近更新 更多