我试图允许用户在selectizeInput中键入值以从长列表中查找他们要搜索的内容(从而避免了滚动操作)。当用户删除默认值“ None”(在此示例中)时,他们将被踢出输入框,在其中必须返回并键入要查找的内容。有没有一种方法可以避免这种情况,以便用户可以退格“无”以将其删除并搜索一个值而不会被开箱即用?

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)

最佳答案

您可以通过optionsselectizeInput()参数使用任何Selectize JS插件。

注意只有第一个输入被更新。

这是代码:

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)


请注意,必须使用isolate()来防止重新调用updateSelectizeeInput()并在用户键入时将所有内容弄乱。

编辑:

对不起队友,我回答时误解了您的期望行为。我认为,如果删除options =,但保留isolate(),您会得到想要的东西。

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


导致updateSelectizeInput()不带isolate()的结果是光标离开输入字段并要求您的用户在删除后重新单击。

让我知道这是否不是您所描述的。干杯!

09-25 21:14