0

I'm pretty new to R Shiny and I want to develop a dashboard to show several histogram plots in two rows, one in the first row and three in the second row. I want the plots in the second row to depend on the choice in the first row. For instance, if the user is choosing "mpg" I wand the second row to show three plots (e.g., "drat", "wt", "qsec") but if "cyl" is selected then only two plots appear in the second column (e.g., "gear", "carb"). I really appreciate your help!


ui <- dashboardPage(
  skin = "yellow",
  dashboardHeader(title = "My dashboard"),
  dashboardSidebar(
    sidebarMenu()
  ),
  dashboardBody(
    fluidRow(
      box(title = "Primary Metric:", status = "primary", solidHeader = T, plotOutput("CPlot"), width = 7),
      box(selectInput("metric", 
                      "Metric:", 
                      c("mpg",  "cyl", "hp", "wt")),  
          width = 3
      )
    ),
    fluidRow(
      box(title = "Sub-Metric1", width = 4,  status = "warning", solidHeader = T, plotOutput("DPlot")),
      box(title = "Sub-Metric2", width = 4,  status = "warning", solidHeader = T, plotOutput("EPlot")),
      box(title = "Sub-Metric3", width = 4,  status = "warning", solidHeader = T, plotOutput("FPlot"))
    )
  )
)


server <- function(input, output){
  
  # plot
  Plot <- reactive({
    ggplot(data, aes(x=data[[input$metric]])) +
      geom_histogram(fill="#D55E00", color="#e9ecef", alpha=0.9) +
      labs(
        title = paste0("Distribution of ", paste0(input$metric), " over the last 12 month"),
        x = req(input$metric),
        y = "Count") +
      theme_minimal()
  }) %>% bindEvent(input$metric)
  
  output$CPlot <-output$DPlot <-output$EPlot <-output$FPlot <- renderPlot({Plot()})
  
  
}

shinyApp(ui = ui, server = server)


user27808
  • 101
  • 7

1 Answers1

0

One way would be using conditionalPanels(), here an example

library(shiny)
library(shinydashboard)
library(magrittr)
library(ggplot2)

ui <- dashboardPage(
  skin = "yellow",
  dashboardHeader(title = "My dashboard"),
  dashboardSidebar(
    sidebarMenu()
  ),
  dashboardBody(
    fluidRow(
      box(title = "Primary Metric:", status = "primary", solidHeader = T, plotOutput("CPlot"), width = 7),
      box(selectInput("metric", "Metric:", c("mpg",  "cyl", "hp", "wt")), width = 3)
    ),
    fluidRow(
      box(title = "Sub-Metric1", width = 4,  status = "warning", solidHeader = T, plotOutput("DPlot")),
      box(title = "Sub-Metric2", width = 4,  status = "warning", solidHeader = T, plotOutput("EPlot")),
      conditionalPanel(
        "input.metric == 'mpg'",
        box(title = "Sub-Metric3", width = 4,  status = "warning", solidHeader = T, plotOutput("FPlot"))
      )
    )
  )
)


server <- function(input, output){
  
  # plot
  Plot <- reactive({
    ggplot(mtcars, aes(x = .data[[input$metric]])) +
      geom_histogram(fill="#D55E00", color="#e9ecef", alpha=0.9) +
      labs(
        title = paste0("Distribution of ", paste0(input$metric), " over the last 12 month"),
        x = req(input$metric),
        y = "Count") +
      theme_minimal()
  }) %>% bindEvent(input$metric)
  
  output$CPlot <-output$DPlot <-output$EPlot <-output$FPlot <- renderPlot({Plot()})
  
  
}

shinyApp(ui = ui, server = server)
Johan Rosa
  • 2,797
  • 10
  • 18