使用条件数据框选择输入

问题描述 投票:0回答:1

我想制作一个闪亮的应用程序,在其中选择不同的数据帧进行可视化。然后我想在同一个数据帧上选择一个过滤器。重点是我希望 selectInput 根据所选数据帧的可用值显示过滤器选项。

这是我的代码,带有 # 的行显示我尝试执行此操作失败。

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)
r shiny shinydashboard selectize.js
1个回答
0
投票

这是一个工作(最小)示例

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(
    cronodata()
  )
}   

shinyApp(ui, server)

© www.soinside.com 2019 - 2024. All rights reserved.