Change sidebar panel with navbarPage in shiny

I am working with sidebarPanel using navbarPage.

I would like to โ€œdeclareโ€ only one widget (radioButtons in this example), and then bind each tab to the โ€œmainโ€ sidebarPanel. As you can see, tab2 does not respond to relative radioButtons. The structure of my project is more complex, having to have the same sidebarPanel for some tabs and a separate sidebarPanel for some other tabs. This is the code I'm using:

library(shiny) server=function(input, output) { output$plot1 = renderPlot({plot(runif(input$rb))}) output$plot2 = renderPlot({plot(runif(input$rb))}) } ui = shinyUI(navbarPage("Test multi page", tabPanel("tab1", sidebarLayout( sidebarPanel( radioButtons("rb","Nr of obs:",choices = c("50 obs"=50,"300 obs"=300)) ), mainPanel(plotOutput("plot1")) ) ), tabPanel("tab2", sidebarLayout( sidebarPanel( radioButtons("rb","Nr of obs:",choices = c("50 obs"=50,"300 obs"=300)) ), mainPanel(plotOutput("plot2")) ) ) )) shinyApp(ui = ui, server = server) runApp("app") 
+5
source share
2 answers

You cannot reuse ui elements the way you want. The closest you can get is to use the conditional panel. In the example below, fixed will be the input you want to have on multiple pages. The conditional panel will allow you to add ui components based on the active tab, where tabid is the label identifier for your tabsetPanel. You don't need to use renderUI and uiOutput here, but it will probably make it easier for you if you have many tabs and ui elements. To see an example, browse this app and click the tabs for the data menu.

 sidebarPanel( uiOutput("fixed"), conditionalPanel("input.tabid == 'tab1'", uiOutput("ui_tab1")), .... ) mainPanel( tabsetPanel(id = "tabid", ... ) 
+2
source

The problem is that you have two input widgets with inputId set to rb . If you change the code in tab2 to:

 radioButtons("rb2","Nr of obs:",choices = c("50 obs"=50,"300 obs"=300)) 

and then connect it to output$plot2 in server :

 output$plot2 = renderPlot({plot(runif(input$rb2))}) 

the application will work.

I suppose the reason is that this is because the brilliant ones will remember the values โ€‹โ€‹of the widgets, even if they are not visible. That way you can easily get one rb value in tab1 and another rb value in tab2 . In this case, it would not be clear what should be built, so the second rb ignored.

0
source

Source: https://habr.com/ru/post/1212094/


All Articles