代码之家  ›  专栏  ›  技术社区  ›  hugh_man

过滤和调整数据表

  •  0
  • hugh_man  · 技术社区  · 3 年前

    我正在尝试显示具有多个 pickerInput 变量。在下面的示例中,按一个变量(物种)进行过滤时,效果很好。然而,当我试图过滤数据帧一秒钟(最终是第三个变量)时,我似乎不知道如何格式化子设置代码

    library(shiny)
    library(data.table)
    
    results <- iris
    results$Species <- as.character(results$Species)
    
    # UI
    ui <- fluidPage(
      
      # Application title
      titlePanel(
        h1("Iris Table", align="center")
      ),
      
      fluidRow( 
        # column(3, 
         #      pickerInput("sepal_width_input", "Sepal", choices = results$Sepal.Width, options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)),
        
        column(3,
               pickerInput("speciesInput", "Species", choices=unique(results$Species), options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)
        ),
        column(9, 
               DT::dataTableOutput('table')))
      
    )
    
    # Server
    server <- function(input, output) {
      
      mydata <- reactive({
        if (is.null(input$speciesInput)) {df <- results
        } else df <- results[results$Species %in% input$speciesInput,]
        df
      })
      
      output$table <- DT::renderDataTable(
        datatable(mydata())
      )
      
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    我在标签后面加了第二个选择器。有人能告诉我,当包含这两个变量时,下面的代码段应该如何格式化吗?

    mydata <- reactive({
            if (is.null(input$speciesInput)) {df <- results
            } else df <- results[results$Species %in% input$speciesInput,]
            df
          })
    
    1 回复  |  直到 3 年前
        1
  •  2
  •   langtang    3 年前

    您可以编写一个处理过滤的函数,并将输入传递给该函数,如下所示:

    library(shiny)
    library(shinyWidgets)
    library(DT)
    
    results <- iris
    results$Species <- as.character(results$Species)
    
    filter_data <- function(spec=NULL, sepw=NULL) {
      res <- results
      if(!is.null(spec)) res <- res %>% dplyr::filter(Species %in% spec)
      if(!is.null(sepw)) res <- res %>% dplyr::filter(Sepal.Width %in% sepw)
      return(res)
    }
    
    # UI
    ui <- fluidPage(
      
      # Application title
      titlePanel(
        h1("Iris Table", align="center")
      ),
      
      fluidRow( 
        column(3, 
              pickerInput("sepal_width_input", "SepalWidth", choices = results$Sepal.Width, options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)),
        
        column(3,
               pickerInput("speciesInput", "Species", choices=unique(results$Species), options = list(`actions-box` = TRUE), selected=NULL, multiple=TRUE)
        ),
        column(9, 
               DT::dataTableOutput('table')))
      
    )
    
    # Server
    server <- function(input, output) {
      
      mydata <- reactive({
        filter_data(input$speciesInput, input$sepal_width_input)
      })
      
      output$table <- DT::renderDataTable(
        datatable(mydata())
      )
      
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)