Plotting the same output in two tabPanels in shiny

后端 未结 2 1364
半阙折子戏
半阙折子戏 2021-01-23 00:42

I\'m trying to plot the same histogram in two separate tab panels within a tabBox in shiny. I can plot the data in one of the tabs, but then when I add the code for the other it

2条回答
  •  死守一世寂寞
    2021-01-23 01:13

    When you create a shiny app, you are creating a HTML site and the outputs are in div containers with ids. So what you are trying without knowing is to create two div container with the same id. This will not work. For a discussion, see here: Can multiple different HTML elements have the same ID if they're different elements?

    What you can do is to wrap the server code in a lapply()function and generate two ids:

    lapply(1:2, function(nr){
      output[[paste0("plot", nr)]] <- renderPlot({hist(mtcars$mpg)})      
    })
    

    and then call plotOutput("plot1") and plotOutput("plot2"). There are also other possibilities to use only one output in a combination with conditionalPanels(), but i think this way should work better for you.

提交回复
热议问题