Problem with selectInput reactivity using dynamic UI and github shiny (version 0.8 works fine)

2,161 views
Skip to first unread message

kevbo...@gmail.com

unread,
Mar 9, 2014, 1:53:50 AM3/9/14
to shiny-...@googlegroups.com
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.

Kevin Lindquist

unread,
Mar 12, 2014, 1:33:07 AM3/12/14
to shiny-...@googlegroups.com
Hello All,

I think I figured this out. In earlier versions of shiny a dot character was allowed in the inputId of a selectInput control. WIth the new selectize controls, if the inputId contains a dot...the input no longer functions properly. I'm unsure if this is intended behavior. It seems like a bug. I'm using the current github shiny on Mac OS X Mavericks.

ui.R

library(shiny)

shinyUI(pageWithSidebar(
    headerPanel("Selectize test"),
    sidebarPanel(
        h4("Select a field:"),
        uiOutput("field_selector_control")
    ),
    mainPanel(
        h4("Selected field:"),
        textOutput("text")
    )
))

Broken selectize, server.R

library(shiny)

shinyServer(function(input,output) {

    output$field_selector_control <- renderUI({
        fields <- names(iris)
        selectInput("field_selector.foo", "Select a field:", fields, selectize = TRUE, multiple = TRUE)
    })

    output$text <- renderText({ paste( input$field_selector.foo, collapse = ", ") })

})

Setting selectize to FALSE fixes the problem, server.R

library(shiny)

shinyServer(function(input,output) {

    output$field_selector_control <- renderUI({
        fields <- names(iris)
        selectInput("field_selector.foo", "Select a field:", fields, selectize = FALSE, multiple = TRUE)
    })

    output$text <- renderText({ paste( input$field_selector.foo, collapse = ", ") })

})

Replacing the dot with an underscore in the inputId fixes the problem (even with selectize = TRUE), server.R

library(shiny)

shinyServer(function(input,output) {

 library(shiny)

shinyServer(function(input,output) {

    output$field_selector_control <- renderUI({
        fields <- names(iris)
        selectInput("field_selector_foo", "Select a field:", fields, selectize = TRUE, multiple = TRUE)
    })

    output$text <- renderText({ paste( input$field_selector_foo, collapse = ", ") })

})

Yihui Xie

unread,
Mar 12, 2014, 12:27:29 PM3/12/14
to Kevin Lindquist, shiny-discuss
Thanks for the report. We have been hoping to emit warnings about
special characters such as the dots in the id's:
https://github.com/rstudio/shiny/issues/358 In general, you should
avoid these meta-characters in the input id:
http://api.jquery.com/category/selectors/

Regards,
Yihui
Reply all
Reply to author
Forward
0 new messages