selectizeinput reading column-name instead of value

Hi everybody,
can somebody explain this behavior to me?
In R, if list l[[i]] contains only one element, selectizeinput shows only the column-name instead of the element. It doesn't include the column name if there are more elements.

In the console the behavior is also different between these 2:

    > l[[1]][,'value']
    
                value 
        "some string" 
 

    > l[[2]][,'value2']
    [1] "more string"  "other string"

    library(shiny)
    
    l <- c()
        
    value <- "some string"
    id <- "1"
    df <- cbind(id,value)
    l[[1]] <- df
    
    value2 <- c("more string", "other string")
    id2 <- c("2","3")
    df2 <- cbind(id2,value2)
    l[[2]] <- df2
    
    ui <- fluidPage(
      selectizeInput("input", "selectize 1", l[[1]][,'value'], multiple=TRUE),
      verbatimTextOutput("out"),
      selectizeInput("input2", "selectize 2", l[[2]][,'value2'], multiple=TRUE),
      verbatimTextOutput("out2")
    )
    
    server <- function(input,output){
      output$out <- renderText({
        input$input
      })
      output$out2 <- renderText({
        input$input2
      })
    }
    
    shinyApp(ui = ui, server = server)

the selectizeInput documentation says

choices List of values to select from. If elements of the list are named, then that name rather than the value is displayed to the user. This can also be a named list whose elements are (either named or unnamed) lists or vectors. If this is the case, the outermost names will be used as the "optgroup" label for the elements in the respective sublist. This allows you to group and label similar choices. See the example section for a small demo of this feature.

so there is specific behaviour difference intended between lists with names and lists without.
Your way of setting up the choices relies on cbind'ing to form matrices.
It might be preferred to work more directly with lists qua lists.

consider:

library(shiny)
l <- c()


l[[1]] <- list(value="some string")

l[[2]] <- list(value=c("more string", "other string"))

l[[3]] <- list(value1="more string",
               value2 ="other string")

ui <- fluidPage(
  selectizeInput("input", "selectize 1", l[[1]], multiple=TRUE),
  verbatimTextOutput("out"),
  selectizeInput("input2", "selectize 2", l[[2]], multiple=TRUE),
  verbatimTextOutput("out2"),
  selectizeInput("input3", "selectize 3", l[[3]], multiple=TRUE),
  verbatimTextOutput("out3")
)

server <- function(input,output){
  output$out <- renderText({
    input$input
  })
  output$out2 <- renderText({
    input$input2
  })
  output$out3 <- renderText({
    input$input3
  })
}

shinyApp(ui = ui, server = server)
1 Like

Thank you so much for helping me out again!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.