[Shiny]: Add link to another tabPanel in another tabPanel

后端 未结 1 1296
悲哀的现实
悲哀的现实 2021-01-06 04:19

I\'m trying to put a link on my \"home\" tabPanel to all others tabPanels of my app.

The idea is as follows:

ui = navbarPage(\"\",
         tabPanel         


        
相关标签:
1条回答
  • 2021-01-06 05:05

    This answer is purely JavaScripted, but very minimal, I guess. Since Shiny creates tabs with random number Ids, and does not give access to the Ids it used, this has do be done client-sided. But there is no knowledge of JavaScript needed to implement this to other scenarios. The JavaScript part is just for Copy/Paste and the trigger command is easy to understand.

    What did I do? I installed a function, that finds the Navbar link corresponding to the desired tab, and just clicks it. This utility can be added to any element with the "onclick" attribute. There are no special tags (e.g. no "a" tag) required.

    The code below should make it easy to customize this solution to fit your needs.

    Note: I used the original Code with the box, although it does not have any visual effect.

    Code:

    library(shiny)
    library(shinydashboard)
    
    ui = shinyUI(
    
      navbarPage("Header",
        tabPanel("home",
          tags$head(tags$script(HTML('
            var fakeClick = function(tabName) {
              var dropdownList = document.getElementsByTagName("a");
              for (var i = 0; i < dropdownList.length; i++) {
                var link = dropdownList[i];
                if(link.getAttribute("data-value") == tabName) {
                  link.click();
                };
              }
            };
          '))),
          fluidPage(
            fluidRow(box("this 1st box should lead me to tab1a", onclick = "fakeClick('tab1a')")),
            fluidRow(box("this 2nd box should lead me to tab1b", onclick = "fakeClick('tab1b')")),
            fluidRow(box("this 2nd box should lead me to tab2", onclick = "fakeClick('tab2')"))
          )
        ), 
        navbarMenu("tab1",
          tabPanel("tab1a", "Some Text inside Tab 1a."),
          tabPanel("tab1b", "Some Text inside Tab 1b.")
        ),
    
        tabPanel("tab2", "Some Text inside Tab 2.")
      )
    )
    
    server = function(input, output, session){}
    
    runApp(shinyApp(ui, server), launch.browser = TRUE)
    

    Have fun!

    0 讨论(0)
提交回复
热议问题