Hello All,
Here is a small app that shows a problem that I have encountered. In this app, there is a selectInput that lists the fields found in a dataframe (I'll call this the
field selector). Then, for each selected field, another selectInput is created...where each selectInput lists the unique values for a given field (I'll call these the
value selectors). Then, the contents of the dataframe are filtered based on the values that are selected (I'm using the iris data set in this example). So, the user can select only the values for each field that are desired for viewing (in a DataTable, plot, etc.).
This works fine on shiny v0.8; however, on the current github (v0.9 candidate) the output data table does not react to changes in the
value selectors. When using v0.9, I see that the
field selector has the behavior and appearance of a selectize widget (selectize.js is a new feature). However, the
value selectors have the behavior and appearance of the older selectInput widget...perhaps that's a hint as to the cause of the problem.
Here is the code.
ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Dynamic data filter test"),
sidebarPanel(
h4("Fields"),
uiOutput("field_selector_control"),
h4("Select value(s) for each field"),
uiOutput("field_values_controls")
),
mainPanel(
dataTableOutput("data_table")
)
))
server.R
library(shiny)
shinyServer(function(input,output) {
output$field_selector_control <- renderUI({
fields <- names(iris)
selectInput("field_selector", "Select a field:", fields, multiple = TRUE)
})
output$field_values_controls <- renderUI({
field_selections <- input$field_selector
sapply(unlist(field_selections), function(x) {
selectInput(paste0( x ,".siw"), paste0("Select - ", x, ":"), unique(iris[[x]]), multiple = TRUE)
})
})
output$data_table <- renderDataTable({
field_selections <- input$field_selector
field_select_inputs <- paste0(field_selections, ".siw")
selected_values_of <- lapply(field_select_inputs, function(x) { input[[x]] })
names(selected_values_of) <- field_selections
data_table <- iris
for(f in field_selections) {
if(!is.null(selected_values_of[[f]])) {
is_in_set <- data_table[[f]] %in% selected_values_of[[f]]
data_table <- data_table[ is_in_set, ]
}
}
# Used for working around a DataTable bug in shiny <= 0.8
# https://github.com/rstudio/shiny/issues/299
if(nrow(data_table) == 0) {
return(data.frame("Empty" = c(" ")))
} else {
return(data_table)
}
})
})
I'm wondering if there is a work around or if this is considered a bug? Thanks for any help.