Shiny's tabsetPanel not displaying plots in multiple tabs

半城伤御伤魂 提交于 2019-12-19 03:39:13

问题


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?


回答1:


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") )
    )
  ))
))



回答2:


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

tabsetPanel(
  tabPanel( "Plot", plotOutput("distPlot1"),plotOutput("distPlot2") )
)


来源:https://stackoverflow.com/questions/22927741/shinys-tabsetpanel-not-displaying-plots-in-multiple-tabs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!