Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny selectizeInput: Read current text

Tags:

r

shiny

With the Shiny selectizeInput widget, the user can type in text as well as select a value from a list of values. Is there a way in R to read the current value of the text?

(Added) I should make it clear that I want to be able to read the text the user enters before he has made a selection. As zimia points out, after he has made a selection, the value of whatever he selected becomes available as input$input_id (assuming that the selectize input has the id "input_id").

like image 829
Daryl McCullough Avatar asked Jan 20 '26 10:01

Daryl McCullough


2 Answers

you can just use the input$input_id. See below

ui <- fluidRow(
  selectizeInput("input1","Enter Text",choices=c("A","B") ,options = list(create=TRUE)),
  textOutput("output1")
)

server<- function(input, output, session){
  output$output1 <- renderText({
    req(input$input1)
    input$input1
  })
}
like image 76
zimia Avatar answered Jan 23 '26 21:01

zimia


Shiny's selectizeInput still gives access to the internal properties, methods and callbacks of Selectize.js through "options" argument of updateSelectizeInput. Selectize.js exposes callback "onType", so you can use like that:

updateSelectizeInput(
  session,
  'input1',
  options = list(
    onType = I('
      text => Shiny.setInputValue("input1_searchText", text)
    '),
    onBlur = I('
      () => Shiny.setInputValue("input1_searchText", null)
    ')
  )
)

and consume it with observer:

observeEvent(input$input1_searchText, {
  print(input$input1_searchText)
  }, ignoreNULL = FALSE
)

Please, note, that I was also interested to know when the select looses focus and hence used "onBlur" event to set the search text to NULL.

like image 44
Daniel Lewandowski Avatar answered Jan 23 '26 20:01

Daniel Lewandowski