SelectizeInput with multiple=TRUE and a placeholder. How do I set a '0' value to the placeholder.

Hi, I have created a Shiny app. But there is one part does not quite work as expected.

Objective: How do I set a 0 value to placeholder?
The purpose is when I am not selecting anything, I want this field to be set to 0.
Apparently this bit works well but when I set multiple = TRUE with a maxItems=5, that has become NULL when I am not selecting anything. And worst case is... this does not work in a condition filter

  1. I added a dummy value in the choices to make it as 0.
    But is that something I can do in the onInitialize to set a var value?

Extract.....

updateSelectizeInput(session,'CUSTOMER_ID',choices=c('Please select : '='0',sort(unique(data_1$cust_id))),selected='0',
options = list(
maxItems=5,
placeholder = 'Please select an option below:',
onInitialize = I('function() { var val = "0"; this.setValue(val); }')),
server=TRUE)

Any help would be great. Thanks in advance.

One approach could be to create a new reactive that checks the value of CUSTOMER_ID. If it's NULL, the reactive is 0, otherwise it takes on the value. Then, instead of referencing input$CUSTOMER_ID throughout your app, you can reference customer_id().

customer_id = reactive({
  if(is.null(input$CUSTOMER_ID)) {
    0
  } else {
    input$CUSTOMER_ID
  }
})
library(shiny)

ui <- fluidPage(
  selectizeInput(
    "mysel",
    label = "This is mysel",
    choices = NULL
  )
)

server <- function(input, output, session) {
  updateSelectInput(
    inputId = "mysel",
    choices = c(0, mtcars$cyl),
    selected = 0
  )
  observeEvent( # check if you clear mysel and need it put to zero
    input$mysel,
    if (!isTruthy(input$mysel)) {
      updateSelectInput(
        inputId =
          "mysel", selected = 0
      )
    }
  )
}

shinyApp(ui, server)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.