Reputation: 115
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)
Upvotes: 1
Views: 58
Reputation: 3152
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)
Upvotes: 2