【发布时间】:2020-08-14 21:27:34
【问题描述】:
尝试根据选中的复选框过滤数据框中的数据。 举个例子:
if (interactive()) {
library(DT)
Dish <- c("Apple Pie", "Apple Cake", "Blueberry Pie", "Lemon", "Carrot", "Chocolate")
DishNum <- c("123", "456", "789", "12", "34", "56")
data <- data.frame(Dish, DishNum)
ui <- fluidPage(
checkboxGroupInput(inputId = "chkDish",
label = "",
inline = TRUE,
choices = c("Apple", "Blue", "Not Apple or Blue"),
selected = c("Apple", "Blue", "Not Apple or Blue")
),
DT::dataTableOutput(outputId = "DT_Dish")
)
server <- function(input, output, session) {
output$DT_Dish <- DT::renderDataTable({
DT::datatable(
data,
rownames = FALSE,
options = list(
dom = 't',
searchHighlight = TRUE,
pageLength = 100,
scrollX = TRUE
)
)
})
}
shinyApp(ui, server)
}
如果仅选中“Apple”框,则只会显示以“Apple”开头的表格中的数据。 如果仅选中“蓝色”框,则只会显示表格中以“蓝色”开头的数据。 如果只勾选“Not Apple or Blue”复选框,它只会显示表格中不以“Apple”或“Blue”开头的数据。
如果选中任何按钮组合,它将适当地过滤/显示数据。
我知道我需要使用一些通配符来对数据进行子集化,但不确定解决此问题的最佳方法。谢谢!
【问题讨论】: