使用R Shiny中的checkboxInput在所有列中选择相同的变量

时间:2018-03-20 20:43:37

标签: r shiny

我想在这些列中过滤具有特定值的所选选定列。我已尝试使用checkboxgroupinput执行此操作。在我的数据集中,这些列的值为0,1。所以我想只选择值为1的列。

library(shinydashboard)
library(shiny)
library(dplyr)
ui <- fluidPage(
  checkboxGroupInput("variable", "Variables to show:",
                     choices = names(mtcars)),
  tableOutput("data")
)

server <- function(input, output, session) {
  data<-reactive({

 mtcars[input$variable==1.00]


  })
  output$data <- renderTable({
   data()
  }, rownames = TRUE)

}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

当您访问checkboxGroupInput时,您会看到一个带有所选项目名称的向量。您可以使用它来选择数据框中的列,然后对它们应用条件:

mtcars[input$variable] == 1

# if "vs" and "am" are checked, this is equivalent to:

mtcars[c('vs','am')] == 1

                       vs    am
Mazda RX4           FALSE  TRUE
Mazda RX4 Wag       FALSE  TRUE
Datsun 710           TRUE  TRUE
Hornet 4 Drive       TRUE FALSE
Hornet Sportabout   FALSE FALSE
Valiant              TRUE FALSE
Duster 360          FALSE FALSE
Merc 240D            TRUE FALSE
Merc 230             TRUE FALSE
...

下一步取决于您是否要选择所选列的任何与值匹配的行或所有列。您可以逐行apply anyall功能将此2D数据帧缩减为可用于选择行的单个逻辑向量。出于您的目的,只需将c('vs','am')替换为input$variable

即可
# Selects rows where ALL of the selected columns have a value of 1
mtcars[apply(mtcars[c('vs','am')] == 1, 1, all),]

                mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

# Selects rows where ANY of the selected columns have a value of 1
mtcars[apply(mtcars[c('vs','am')] == 1, 1, any),]

                    mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Valiant        18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Merc 240D      24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230       22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280       19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C      17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Maserati Bora  15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2