1

I have a shiny app with a sidebarpanel and a main panel with multiple tabs. I want to collapse the sidebarpanel for some tabs and show it for some tabs.

How can I do the same? Thanks.

user1463242
  • 137
  • 3
  • 12
  • 2
    http://stackoverflow.com/questions/42159804/how-to-collapse-sidebarpanel-in-shiny-app – TinaW Feb 19 '17 at 22:28
  • I don't want to use an action button to collapse the sidebar. So, if a user is on tab1, show the sidebar panel. If the user goes to tab2, collapse/hide the sidebar panel. – user1463242 Feb 19 '17 at 22:33
  • to check on which tab a user is use: `if(input$tabs == "tabname")` – Tonio Liebrand Feb 19 '17 at 22:34

1 Answers1

3

I am not really sure you really need to "hide" or just specify for some tabs a sidebar and for some not (see the ui part). In case you need to hide a sidebar see the (commented) server part.

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  navbarPage("",
             tabPanel("tab",
                      div( id ="Sidebar",sidebarPanel(
                        actionButton("showSidebar", "I am tab sidebar content")
                      )),


                      mainPanel(actionButton("showSidebar", "I am tab main content")
                      )
             ),
             tabPanel("tab2",
                      div( id ="Sidebar2",sidebarPanel(
                        actionButton("showSidebar", "I am tab2 sidebar content")
                      )),


                      mainPanel(actionButton("showSidebar", "I am tab2 main content")
                      )
             ),
             tabPanel("tab3",
                      mainPanel(actionButton("showSidebar", "I dont have a sidebar")
                      )
             )

  )
)

server <-function(input, output, session) {
  # In case you need to hide them for some reason
  # observeEvent(input$tabs == "tab", {
  #   shinyjs::hide(id = "Sidebar")
  # })
}

shinyApp(ui, server)  
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • 1
    I do need to hide. This works perfectly. Thanks! – user1463242 Feb 19 '17 at 22:47
  • 1
    Actually I never thought of having individual sidebar and main panel for each tab. – user1463242 Feb 19 '17 at 22:50
  • Well, yeah kind of a trade off between repeating yourself and having the possibility to customize your sidebar per tab :-) – Tonio Liebrand Feb 19 '17 at 22:52
  • Hey, I was actually trying to put an action button in the same shiny app but it does't seem to work. Here is the code- observeEvent(input$submit2, { updateNavbarPage(session, "Tabs",selected = "Tab1") }) Here "Tabs" is my id for navbarpage. It is working when I am using the click command on a plot to move across tabs. – user1463242 Feb 22 '17 at 12:42
  • a bit difficult to discuss in the comment section, could you open a new question – Tonio Liebrand Feb 22 '17 at 18:41