在R Shiny中通过checkboxGroupInput选择多个列

时间:2018-06-25 14:12:50

标签: r checkbox shiny grepl

我有一个问题,如何在R Shiny中通过checkboxGroupInput在数据集中选择多个列。

现在我的数据集具有这样的一列:(模式为stateName / number / number)

IndividualName

SA / 111111/222222

VIC / 33333/444444

NSW / 55555/666666

QLD / 777777/888888

.....

,我有一个很好的选择框。我使用grepl提取状态名称,并且可以成功选择单个状态。

UI:

        selectInput("select_state", h3("Select State"),
                choices = list("All States"="SA|VIC|NSW|QLD|WA|TAS|NT|ACT|CTH","South Australia"="SA",
                               "Victoria"="VIC","New South Wales"="NSW","Queensland"="QLD",
                               "Western Australia"="WA","Northern Territory"="NT","Tasmania"="TAS",
                               "Australian Capital Territory"="ACT","Commonwealth"="CTH")),

服务器:

entities_state <- entities[ with(entities, grepl(input$select_state, c(entities$IndividualName))), ]

现在我想将选择框更改为复选框组,我知道要使用复选框组,我们可以编写

entities_state <-filter(entities, IndividualName %in% input$select_state)

但是我仍然需要从“ IndividualName”列中提取stateName关键字。我不知道如何结合使用grepl,filter和%n%来使复选框组起作用。

希望我能清楚地表达我的问题。如果没有,请告诉我。

1 个答案:

答案 0 :(得分:0)

可以使用patterngreplpaste来制作collapse的{​​{1}},这样它将检查选择的任何一个与|

select_state

或者我们可以在i1 <- grepl(paste(input$select_state, collapse="|"), entities$IndividualName) library(dplyr) entities %>% filter(i1)

中创建它
filter