【问题标题】:Filter data using varSelectInput in Shiny?在 Shiny 中使用 varSelectInput 过滤数据?
【发布时间】:2021-09-29 04:42:24
【问题描述】:

我想使用用户选择的变量来过滤(然后绘制)数据。但是我收到一个错误,即不能强制“list”对象键入“double”。这是一个虚拟示例,我想说:“如果用户从变量列表中选择“齿轮”,则显示齿轮 = 4 的所有汽车的马力直方图。

library(shiny)

mtcars_cols <- mtcars %>% select(gear, carb)

# Define UI for application that draws a histogram
ui <- fluidPage(
   
   # Application title
   titlePanel("Motor Car Trend Road Tests"),
   
   # Sidebar 
   sidebarLayout(
      varSelectInput("variables", 
                     "Select variables: ", 
                     mtcars_cols,
                     multiple = TRUE),
      
      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
   
   output$distPlot <- renderPlot({
      dt <- mtcars %>%
        filter(!!!input$variables == 4)
      
      bins <- seq(min(x), max(x), length.out = 10 + 1)
      
      # draw the histogram with the specified number of bins
      hist(dt$hp, breaks = bins, col = 'darkgray', border = 'white')
   })
}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r filter dplyr shiny


    【解决方案1】:

    我认为区分varSelectInput 与multiple = TRUE 和multiple = FALSE 很重要。让我们从后一个开始(看看打印中间步骤的控制台)。只有一个变量input$variables 是一个名称。所以我们可以在dplyr::filter中使用!! input$variables == 4。

    library(shiny)
    library(dplyr)
    
    mtcars_cols <- mtcars %>% select(gear, carb)
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
      
      # Application title
      titlePanel("Motor Car Trend Road Tests"),
      
      # Sidebar 
      sidebarLayout(
        varSelectInput("variables", 
                       "Select variables: ", 
                       mtcars_cols,
                       multiple = FALSE),
        
        # Show a plot of the generated distribution
        mainPanel(
          plotOutput("distPlot")
        )
      )
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
      
      output$distPlot <- renderPlot({
        
        print(input$variables)
        
        dt <- mtcars %>%
          filter(!! input$variables == 4)
        
        print(dt)
    
        bins <- seq(min(dt$hp), max(dt$hp), length.out = 10 + 1)
        
        # draw the histogram with the specified number of bins
        hist(dt$hp, breaks = bins, col = 'darkgray', border = 'white')
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    当我们有多个变量时,情况就不同了。这里input$variables 是一个名称列表。如果我们想过滤所有选定变量都相等4 的所有行,一个好方法是使用purrr::map 和bquote 创建一个表达式列表。我们可以在dplyr::filter 中使用这个表达式列表,例如!!! expr_ls。

    library(shiny)
    library(dplyr)
    
    mtcars_cols <- mtcars %>% select(gear, carb)
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
      
      # Application title
      titlePanel("Motor Car Trend Road Tests"),
      
      # Sidebar 
      sidebarLayout(
        varSelectInput("variables", 
                       "Select variables: ", 
                       mtcars_cols,
                       multiple = TRUE),
        
        # Show a plot of the generated distribution
        mainPanel(
          plotOutput("distPlot")
        )
      )
    )
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
      
      output$distPlot <- renderPlot({
        
        req(input$variables)
        print(input$variables)
        
        expr_ls <- purrr::map(input$variables, ~ bquote(.(.x) == 4))
        
        dt <- mtcars %>%
          filter(!!! expr_ls)
    
        print(dt)
    
        bins <- seq(min(dt$hp), max(dt$hp), length.out = 10 + 1)
    
        # draw the histogram with the specified number of bins
        hist(dt$hp, breaks = bins, col = 'darkgray', border = 'white')
      })
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 很好的答案。非常感谢区分input$variables 是名称还是列表。
    猜你喜欢
    • 1970-01-01
    • 2019-01-11
    • 2020-03-26
    • 2019-06-12
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    相关资源
    最近更新 更多