动态selectizeInput闪亮

时间:2018-07-14 21:33:33

标签: r shiny

我试图允许用户在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)

1 个答案:

答案 0 :(得分:1)

您可以通过options的{​​{1}}参数使用任何Selectize JS插件。

这里是live example。请注意,只有第一个输入被更新。

代码如下:

selectizeInput()

请注意,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") 的{​​{1}}导致光标离开输入字段,并要求用户在删除后重新单击。

我更新了现场演示here,请问这是否不是您要描述的内容。干杯!