manu p
manu p

Reputation: 985

Capture what is selected in selectizeinput

I am trying to capture the what we have selected in selectizeinput (for example if we select 3, then 3 should be printed), but here, it goes on printing

library(shiny)
library(DT)

init.choices <- c("Choose From List or Type New Value"="")

ui <- fluidPage(
  selectizeInput("foo", NULL, 
                 choices=init.choices,
                 multiple=FALSE,
                 options=list(create=TRUE))
)

server <- function(input, output, session) {
  observe({
    updateSelectizeInput(session, "foo", choices=c(init.choices,  c(1,2,3)), selected = 3, server=TRUE)
    print(input$foo)
  })
}

shinyApp(ui, server)

Upvotes: 0

Views: 21

Answers (1)

gdevaux
gdevaux

Reputation: 2505

The printing is wrong because you put print(input$foo) in the same observe than the updateSelectizeInput. Put it in a different observe. And if you only want to print when it has a value, and no NULL or "", you can add req(input$foo) before the print.

library(shiny)
library(DT)

init.choices <- c("Choose From List or Type New Value"="")

ui <- fluidPage(
  selectizeInput("foo", NULL, 
                 choices=init.choices,
                 multiple=FALSE,
                 options=list(create=TRUE))
)

server <- function(input, output, session) {
  observe({
    updateSelectizeInput(session, "foo", choices=c(init.choices,  c(1,2,3)), selected = 3, server=TRUE)
  })
  
  observe({
req(input$foo)
print(input$foo)
})
}

shinyApp(ui, server)

Upvotes: 1

Related Questions