在闪亮的R中使用selectInput来选择和过滤行,而不是选择列

时间:2018-05-15 11:34:42

标签: r shiny

我正在尝试使用有光泽的selectInput函数来过滤数据框中的行,例如“mtcars”。因此,用户将选择他想要查看的汽车,而outputTable将仅显示所选汽车的统计数据。在selectInput示例中,选择适用于列:

## Only run examples in interactive R sessions
if (interactive()) {

# basic example
shinyApp(
  ui = fluidPage(
    selectInput("variable", "Variable:",
                c("Cylinders" = "cyl",
                  "Transmission" = "am",
                  "Gears" = "gear")),
    tableOutput("data")
  ),
  server = function(input, output) {
    output$data <- renderTable({
      mtcars[, c("mpg", input$variable), drop = FALSE]
    }, rownames = TRUE)
  }
)
}

但是,我正在尝试使用dplyr根据用户输入选择的汽车过滤掉汽车:

library(shiny)
library(dplyr)

#####Just adding a column name to mtcars for the cars column####
#cars <- mtcars
#mtcars <-setNames(cbind(rownames(cars), cars, row.names = NULL), 
#                  c("cars", "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"))

if (interactive()) {

  shinyApp(
    ui = fluidPage(
      selectInput("variable", "Pick a Car: ",
                  c("All" = "All Cars",
                    "Ford" = "Ford",
                    "Volvo" = "Volvo",
                    "Ferrari" = "Ferrari",
                    "Fiat" = "Fiat",
                    "Merc" = "Merc")),
      tableOutput("data")
    ),


    server = function(input, output) {

      output$cars <- renderText({
        mtcars %>%
          filter(mtcars$cars %in% as.character(input$variable))
      })

      output$data <- renderTable({
        output$cars
      })

    }
  )
}

任何建议都将不胜感激!

2 个答案:

答案 0 :(得分:2)

您只需要renderTable()并将过滤后的数据框传递给它 要过滤数据框,我们需要找到rowname(或添加的列input$cars)中存在car的行。
但是在选择All时这不会起作用,这就是我们使用条件input$cars == 'All Cars'的原因。

这应该有效:

shinyApp(
  ui = fluidPage(
    selectInput("cars", "Pick a Car: ",
                c("All" = "All Cars",
                  "Ford" = "Ford",
                  "Volvo" = "Volvo",
                  "Ferrari" = "Ferrari",
                  "Fiat" = "Fiat",
                  "Merc" = "Merc")),
    tableOutput("data")
  ),


  server = function(input, output) {

    mtcars$car <- rownames(mtcars)

    output$data <- renderTable({
      mtcars %>%
        filter(stringr::str_detect(car, as.character(input$cars)) | input$cars == 'All Cars')
    })

  }
)

enter image description here

答案 1 :(得分:1)

首先,第二个示例中的输入$变量未知,因为您的selectInput-ID是“cars”。然后你不使用renderedText输出,所以过滤永远不会发生。最后,mtcars数据中没有可能的选择。无论如何,它永远不会找到该表的任何数据。

我写了一个可重复的示例,将可选择的选项更改为mtcars数据的实际名称。

rm(list=ls())
cars <- mtcars
mtcars <-setNames(cbind(rownames(cars), cars, row.names = NULL),
                 c("cars", "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am", "gear", "carb"))
library(shiny)
library(DT)
if (interactive()) {

  shinyApp(
    ui = fluidPage(
      selectInput(inputId = "variable", label = "Pick a Car: ",choices = 
                    c("Mazda RX4 Wag" = "Mazda RX4 Wag",
                    "Merc 280" = "Merc 280",
                    "Volvo 142E" = "Volvo 142E",
                    "Duster 360" = "Duster 360",
                    "Lotus Europa" = "Lotus Europa"), selected = "Mazda RX4 Wag"),
      verbatimTextOutput("cars"),
      tableOutput("data")
    ),


    server = function(input, output) {

      carReact <- reactiveValues(car=NULL)

      output$cars <- renderText({
        txt <- mtcars[mtcars$cars %in% as.character(input$variable),]
        ## Use regex() or match() for example, if you only want partial matching of the car names.

        carReact$car <- txt
        paste(txt, collapse = ";")
      })

      output$data <- renderTable({
        req(carReact$car)
        datatbl <- carReact$car
        as.data.frame(datatbl)
      })

    }
  )
}

所选车辆名称保存在reactiveValue(carReact)中,并在renderText函数中指定。而renderText无法打印出列表,因此您只需将其转换为1个变量或使用paste()函数。

相关问题