Multiple uiOutput With Two Filters

I have a shiny app that has three tabs:

1st tab: imports data
2nd tab: dynamic boxplot based on the selected value in a column (id)
3rd tab: dynamic boxplot based on the selected date range (date column)

My app works fine. However, I'd like to add one more function to my 2nd tab which is the date range. So, I want to have two filters in my 2nd tab. For instance, I first choose id A and after that I choose a date range and draws a boxplot. I included a sample data.

df <- data.frame("date" = c("2020-01-01", "2020-01-01", "2020-01-02", "2020-01-02", "2020-01-03"), "id" = c("A", "A", "B", "B", "C"), "number" = c(1, 3, 2, 5, 4))

write.csv(df, "df.csv", row.names = F)

library(shiny)
library(dplyr)
library(ggplot2)

if (interactive()) {
  ui <- fluidPage(
    tabsetPanel(
      tabPanel("Import Data", fileInput("file", "Upload Your File:")),

      # How do I add one more uiOutput here?
      tabPanel("Number By Table", uiOutput("ids"), plotOutput("plot1")),  
      tabPanel("Number By Date", uiOutput("daterange"), plotOutput("plot2"))
)
)       
  
  server <- function(input, output, session) {
    df <- reactive({
      req(input$file)
      inFile <- input$file
      read.csv(inFile$datapath, stringsAsFactors = F)
    })
    
    output$ids <- renderUI({
      selectizeInput("id",
                     "Select Your ID:", 
                     choices = sort(unique(df()$id)))
    })
    
    output$daterange <- renderUI({
      dates <- as.Date(df()$date)
      dateRangeInput("daterange", "Select the date range:",
                     start = min(dates), end = max(dates),
                     min = min(dates), max = max(dates)
      )
    })
    
    output$plot1 <- renderPlot({
      dfplot1 <- filter(df(), df()$id == input$id)
      # I tried the below code, but it doesn't work
      # dfplot1 <- subset(dfplot1, date >= input$daterange[1] & date <= input$daterange[2])
      
      g1 <- ggplot(dfplot1, aes(date, number)) +
        geom_boxplot()
      print(g1)
      
      
      output$plot2 <- renderPlot({
      dfplot2 <- subset(df(), date >= input$daterange[1] & date <= input$daterange[2])
        
      g2 <- ggplot(dfplot2, aes(id, number)) +
        geom_boxplot()
      print(g2)
      })
    })
  }
  
  shinyApp(ui = ui, server = server)
}

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