Question

I am trying to use multiple tabPanel controls within the tabsetPanel in Shiny. Lets say I start with only one tab using the following code:

mainPanel(
    tabsetPanel(
    tabPanel("Plot",plotOutput("distPlot"))
    )

The code runs fine and displays the plot in the tab.

But the moment I introduce another tab just to test the tabs out, both the tabs stop displaying any plots at all. I am using the following code:

mainPanel(
    tabsetPanel(
    tabPanel("Plot",plotOutput("distPlot")),
    tabPanel("Plot",plotOutput("distPlot"))
    )

Please note that I am trying to display the same plot in both tabs just to test if the tabs work. All I get are two blank tabs (if I use only one tab, the plot displays properly).

Would someone please be able to help me figure this out?

Was it helpful?

Solution

You assign "distPlot" to plotOutput's parameter outputId. "ID" indicates that this value has to be unique over the entire shiny app. You can assign the same plot to two different plotOutputs though:

runApp( list(

  server = function(input, output) {
    df <- data.frame( x = rnorm(10), y = rnorm(10) )
    output$distPlot1 <- renderPlot({ plot( df, x ~ y ) })
    output$distPlot2 <- renderPlot({ plot( df, x ~ y ) })
  },

  ui = fluidPage( mainPanel(
    tabsetPanel(
      tabPanel( "Plot", plotOutput("distPlot1") ),
      tabPanel( "Plot", plotOutput("distPlot2") )
    )
  ))
))

OTHER TIPS

we can also write the above code in a single line as

tabsetPanel(
  tabPanel( "Plot", plotOutput("distPlot1"),plotOutput("distPlot2") )
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top