How to make R Shiny DataTable table give columns drop down menus with all possible values populated
I have the following code that generates a R Shiny datatable. There are 开发者_C百科currently free text input boxes for each column that allows you filter the contents. I have been struggling with figuring out how to make these drop down menu filters, that would have all possible options for that given column populated, instead.
I know this current column level search functionality is coming from filter = "top", but I'm struggling to even find documentation on the other possible inputs for this parameter.
Full code below:
ui <- fluidPage(
downloadButton("download_data", "Download filtered data as CSV"),
DT::dataTableOutput("data"),
)
# Define server logic
server <- function(input, output) {
filtered_data <- reactive({
if (length(input$columns) == 0) {
return(dataframe)
} else {
dataframe %>%
filter_at(vars(input$columns), any_vars(grepl(input$search, ., ignore.case = TRUE)))
}
})
output$data <- DT::renderDataTable({
DT::datatable(filtered_data(), filter = "top", escape = FALSE, rownames = FALSE)
})
output$download_data <- downloadHandler(
filename = paste("Land_Use_Data_",toString(Sys.Date()), ".csv", sep = ""),
content = function(file) {
write.csv(filtered_data(), file, row.names = FALSE)
}
)
}
shinyServer(function(input, output, session) {
reactiveValuesToList(session)$search <- ""
})
shinyApp(ui = ui, server = server)
If you want a dropdown list for a column, convert this column to a factor. Example:
dat <- iris
dat$Sepal.Length <- factor(dat$Sepal.Length)
DT::datatable(dat, filter = "top")
精彩评论