Plotting the same output in two tabPanels in shiny

Viewed 1657

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 seems to break the app. Below is an example of I'm trying to do:

    library(shiny)
     library(dplyr)

data(mtcars)

body <- dashboardBody(
  fluidRow(
    tabBox(
      title = "miles per gallon",

      id = "tabset1", height = "250px",
      tabPanel("Tab1", plotOutput("plot1")),
      tabPanel("Tab2", plotOutput("plot1"), "test") # the app 'breaks' when I add in the **plotOutput("plot1")** here... however it works when I remove it
    )
    )
  )

shinyApp(
  ui = dashboardPage(
    dashboardHeader(title = "Test"),
    dashboardSidebar(),
    body
  ),
  server = function(input, output) {


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

  }
)

In this particular example, I could just add another line in the server like this

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

and then call plot2, but my actual app is a bit more complex than the above example, so I'd like to plot plot1 in both tabs.

2 Answers
Related