Shiny app: Keep panels consistent in several views

In Shiny I want to keep some panels (panel1 below) consistent in several views (Plotview 1 and 2 below).

Below I am able to show panel1 in Plotview2 – but, the panel does not work in this view.

(The goal is to be able to have a few "panels" consistent through several "Plotviews" – and a few specific "panels" for some "Plotviews".)


library(shiny)

# Example panels
panel1 <- tabPanel("Panel 1",
                   radioButtons(inputId = "alt1",
                                      label = "Select:",
                                      choices = c(
                                        "A" = "A",
                                        "B" = "B"),
                                      selected = "A"))

panel2 <- tabPanel("Panel 2",
                   radioButtons(inputId = "alt2",
                                      label = "Select:",
                                      choices = c(
                                        "C" = "C",
                                        "D" = "D"),
                                      selected = "C"))
# UI
ui <-
  navbarPage(title = "Panel 1-2",
             collapsible = TRUE,
             fluid = TRUE,

             # tab 1
             tabPanel("Plotview 1",
                      fluidRow(
                        column(3,
                               tabsetPanel(type = "pills",
                                           panel1)),

                        # Main panel 1
                        mainPanel(fluidRow(
                          plotOutput("plot1"))
                        ))),

             # tab panel 2
             tabPanel("Plotview 2",
                      fluidRow(
                        column(3,
                               tabsetPanel(type = "pills",
                                           panel1,
                                           panel2,
                                           selected = "Panel 2")),

                        # Main panel 2
                        mainPanel(plotOutput("plot2"))
                        )))

## server
server <- function(input, output) {

  # Example plot 1
  output$plot1 <- renderPlot({

    if (input$alt1[1] == "A") data <- iris$Sepal.Length
    if (input$alt1[1] == "B") data <- iris$Sepal.Width

    hist(data)
  })

  # Example plot 2
  output$plot2 <- renderPlot({

    if (input$alt1[1] == "A") data <- iris$Sepal.Length
    if (input$alt1[1] == "B") data <- iris$Sepal.Width

    if (input$alt2[1] == "C") brk <- 2
    if (input$alt2[1] == "D") brk <- 4

    hist(data, breaks = brk)
  })
}

shinyApp(ui = ui, server = server)

Thank you.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.