Plotting the same output in two tabPanels in shiny

后端 未结 2 1363
半阙折子戏
半阙折子戏 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:30

    BigDataScientist's answer is great and very scalable.

    But for situations where you only have one or two outputs that you want to repeat, I think the easiest and most readable solution is to assign them all to the same render function. For example, this would be:

    output$plot1 <- output$plot2 <- renderPlot({ hist(mtcars$mpg) })
    

    Here's the full working app using this solution:

    library(shiny)
    
    body <- dashboardBody(
      fluidRow(
        tabBox(title = "miles per gallon", id = "tabset1", height = "250px",
          tabPanel("Tab1", plotOutput("plot1")),
          tabPanel("Tab2", plotOutput("plot2"), "test")
        )
      )
    )
    
    shinyApp(
      ui = dashboardPage(
        dashboardHeader(title = "Test"),
        dashboardSidebar(),
        body
      ),
      server = function(input, output) {
        output$plot1 <- output$plot2 <- renderPlot({ hist(mtcars$mpg) })
      }
    )
    

提交回复
热议问题