dk.*_*dk. 3 shiny selectize.js
Shiny 提供了一个selectizeInputwrapper selectize.js,它生成一个文本输入/组合框小部件。我想延迟加载 selectizeInput 的选项/值有几个原因:可能有一长串值,或者可能的值取决于其他参数。另外,我希望用户能够创建新值(例如选择现有值或创建您自己的值)。
But in all of these cases, the value of the selectizeInput cannot be bookmarked using Shiny's server-side bookmarking. When the selectizeInput object is initialized, the bookmarked value stored in the input are not yet valid option values.
Does anybody have any clever workarounds to bookmark selectizeInput values?
Here's the simplest example dealing with just the create option:
library(shiny)
ui <- function(request) {
fluidPage(
selectizeInput("foo", "Created Values Cannot Be Bookmarked", choices=c("Choose From List or Type New Value"="", "bar", "baz"),
options=list(create=TRUE)),
bookmarkButton("Update URL")
)}
server <- function(input, output) {
onBookmarked(function(url) {
updateQueryString(url)
})
}
shinyApp(ui = ui, server = server, enableBookmarking = "server")
Run Code Online (Sandbox Code Playgroud)
If you type a new value in the selection box, hit the bookmark button to update the URL in the location bar in the browser, and hit reload, then that created value will be lost. Try the same with an existing option and it works, of course.
It gets more complicated if option loading is delayed, but the problem is the same, which is that the user's selection is not valid when the selectizeInput is rendering.
这是使用服务器端支持选择的更好解决方案。支持选项的重要步骤create=TRUE是使用onRestored用户创建的新值来更新选项。
library(shiny)
init.choices <- c("Choose From List or Type New Value"="")
ui <- function(request) {
fluidPage(
titlePanel("Bookmarking Created Values"),
selectizeInput("foo", NULL,
choices=init.choices,
multiple=TRUE,
options=list(create=TRUE)),
bookmarkButton("Update URL")
)}
server <- function(input, output, session) {
updateSelectizeInput(session, "foo", choices=c(init.choices, rownames(mtcars)), server=TRUE)
onBookmarked(function(url) {
updateQueryString(url)
})
onRestored(function(state) {
updateSelectizeInput(session, "foo", selected=state$input$foo, choices=c(init.choices, rownames(mtcars), state$input$foo), server=TRUE)
})
}
shinyApp(ui = ui, server = server, enableBookmarking = "server")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
992 次 |
| 最近记录: |