1

I want to make a shiny app in which I select different dataframes to visualize. Then I want to select a filter on this same dataframe. The point is that I want the selectInput to show the filter options according to the available values ​​of the selected dataframe.

This is my code and the lines with # show my unsuccessful attempt to do this.

library(dplyr)
library(tidyverse)
library(data.table)
library(tidyr)
library(shinydashboard)
library(shiny)
library(DT)



ui <- dashboardPage(
        dashboardHeader(title = "Planejamento de compras"), #título do aplicativo
        dashboardSidebar(
            sidebarMenu(
                radioButtons(inputId = "cronograma", label = "Qual ano deseja pesquisar?",  choices = c("Cronograma_2022", "Cronograma_2023"), selected = "Cronograma_2023"),
                selectizeInput(inputId = "setor", label = "Selecione o setor:", choices = NULL, multiple = TRUE)
            )
        ),
        dashboardBody(
                fluidRow(
                box(DT::DTOutput("table"),)
            )
        )
                
)



server <- function(input, output, session) {
        cronodata <- reactive({
            if(input$cronograma == "Cronograma_2022"){ 
                table0 <- iris[Species == "setosa" | Species =="versicolor" ,] #different dataframes with diferent values in Column "Species"
                                
            }else if(input$cronograma == "Cronograma_2023"){
                table0 <- iris [Species == "virginica" | Species =="versicolor",] #different dataframes with diferent values in Column "Species"

            }else{
                break
            }
            #table0 <- table0[Species %in% input$setor,]
            setDT(table0)
            return(table0)
        
        })


        #updateSelectizeInput(session, "setor", choices = unique(sort(cronodata()[,Species,])), server = TRUE)
        
        output$table <- DT::renderDT(cronodata(), 
            
            )
}   

shinyApp(ui, server)

1 Answer 1

2

Here is a working (minimal) example

library(dplyr)
library(shiny)



ui <- fluidPage(
  radioButtons(inputId = "data_frame", label = "Data frame",  choices = c("Cronograma_2022", "Cronograma_2023"), selected = "Cronograma_2023"),
  selectizeInput(inputId = "specie", label = "Select specie", choices = NULL, multiple = TRUE),
  tableOutput("table")
)



server <- function(input, output, session) {
  cronodata <- reactive({
    switch(
      input$data_frame,
      Cronograma_2022 = filter(iris, Species %in% c("setosa", "versicolor")),
      Cronograma_2023 = filter(iris, Species %in% c("virginica", "versicolor")),
      stop()
    )
  })
  
  observe(
    updateSelectizeInput(session, "specie", choices = unique(cronodata()$Species))
  )
  
  output$table <- renderTable({
    req(input$specie)
    cronodata() |>
      filter(Species == input$specie)
  })
}   

shinyApp(ui, server)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Johan, I can already see the update of InputSelectize. However, in the example you created, the table does not update when I select one of the options that are available in inputSelectize.
For example: If I select the species setosa in inputselectize (in this case in dataframe Cronograma_2022) the table does not update. I would like it to update showing only the rows containing Species setosa.
Changes added :)
Thanks Johan! If it's not a problem, would it be possible to add another selectinput whose text box updates as the option in the other selectinput is selected?
You're asking for the same logic that is already in the answer. I think that for simple changes like this one is better if you use the "Shiny assistant" shiny.posit.co/blog/posts/shiny-assistant

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.