Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic selectizeInput in shiny

Tags:

r

shiny

I am trying to allow the user to type the value in the selectizeInput to find what they are searching from a long list (thus avoiding the scrolling action). When the user deletes the default value "None" (in this example), they are kicked out of the input box where they have to go back and type what they are seeking. Is there a way to avoid this so the user can backspace "None" to delete it and search for a value without being pushed out of the box?

library(shiny)
library(shinydashboard)


ui <- dashboardPage(
dashboardHeader(title = "Dynamic selectInput"),
dashboardSidebar(
sidebarMenu(
  menuItemOutput("menuitem")
)
),
dashboardBody(
selectizeInput("heir1","Heirarchy1",c("NONE",letters),selected="NONE"),
selectizeInput("heir2","Heirarchy2",c("NONE",letters),selected="NONE"),
selectizeInput("heir3","Heirarchy3",c("NONE",letters),selected="NONE")
 )
 )

server <- function(input, output, session) {
output$menuitem <- renderMenu({
menuItem("Menu item", icon = icon("calendar"))
})

heirarchy<-c(letters)

observe({
hei1<-input$heir1
hei2<-input$heir2
hei3<-input$heir3

choice1<-c("NONE",setdiff(heirarchy,c(hei2,hei3)))
choice2<-c("NONE",setdiff(heirarchy,c(hei1,hei3)))
choice3<-c("NONE",setdiff(heirarchy,c(hei1,hei2)))

updateSelectizeInput(session,"heir1",choices=choice1,selected=hei1)
updateSelectizeInput(session,"heir2",choices=choice2,selected=hei2)
updateSelectizeInput(session,"heir3",choices=choice3,selected=hei3)

 })

 }

 shinyApp(ui, server)
like image 949
Eric Pios Avatar asked May 22 '26 02:05

Eric Pios


1 Answers

You can use any of the Selectize JS plugins via the options argument to selectizeInput().

Note only the first input is updated.

Here is the code:

library(shiny)
library(shinydashboard)


ui <- dashboardPage(
    dashboardHeader(title = "Dynamic selectInput"),
    dashboardSidebar(
        sidebarMenu(
            menuItemOutput("menuitem")
        )
    ),
    dashboardBody(

        selectizeInput("heir1","Heirarchy1",c("NONE",letters),selected="NONE",
                       # use this syntax to bring in selectize.js plugins :)
                       options = list(plugins = list('restore_on_backspace'))),

        selectizeInput("heir2","Heirarchy2",c("NONE",letters),selected="NONE"),
        selectizeInput("heir3","Heirarchy3",c("NONE",letters),selected="NONE")
    )
)

server <- function(input, output, session) {
    output$menuitem <- renderMenu({
        menuItem("Menu item", icon = icon("calendar"))
    })

    heirarchy<-c(letters)

    observe({

        hei1<-isolate(input$heir1) # don't allow re-evaluation as users type

        hei2<-input$heir2
        hei3<-input$heir3

        choice1<-c("NONE",setdiff(heirarchy,c(hei2,hei3)))
        choice2<-c("NONE",setdiff(heirarchy,c(hei1,hei3)))
        choice3<-c("NONE",setdiff(heirarchy,c(hei1,hei2)))

        updateSelectizeInput(session,"heir1",choices=choice1,selected=hei1)
        updateSelectizeInput(session,"heir2",choices=choice2,selected=hei2)
        updateSelectizeInput(session,"heir3",choices=choice3,selected=hei3)

    })

}

shinyApp(ui, server)

Note that isolate() is necessary to prevent updateSelectizeeInput() from being re-called and messing everything up as your users type.

EDIT:

Sorry mate, misread your desired behavior when I answered. I think you will get what you want if you remove the options =, but keep the isolate().

selectizeInput("heir1","Heirarchy1",c("NONE",letters),selected="NONE")

It is the updateSelectizeInput() without isolate() that is causing the cursor to leave the input field and requiring your users to re-click after a deletion.

Let me know if that's not what you were describing. Cheers!

like image 181
Nate Avatar answered May 24 '26 15:05

Nate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!