R Shiny selectizeInput服务器端未正确初始化

时间:2015-04-30 11:47:29

标签: r widget shiny server-side selectize.js

当尝试在Shiny应用程序中嵌入选择性输入窗口小部件时,我遇到了以下问题:

使用server = TRUE时,项目列表未使用完整的项目列表进行初始化。只有在搜索框中输入字符后才会显示项目。这与server = FALSE时的行为相反。在这种情况下,所有项目最初都显示在框中(这是我想要的行为)。我想使用server = TRUE选项的原因是我希望能够将格式应用于项目,这在生成项目客户端时是不可能的。下面的示例使用server = TRUE和另一个使用server = FALSE的一个选择性输入再现了这种现象。在我看来,两种情况下的行为都应该相同。

示例app.R重现问题:

library(shiny)

server <- function(input, output, session) {
  updateSelectizeInput(session, 'cars1', server = TRUE, choices = rownames(mtcars))  
  updateSelectizeInput(session, 'cars2', server = FALSE, choices = rownames(mtcars))
}

ui <- fluidPage(
  selectizeInput('cars1', label = "server side", choices = NULL),
  selectizeInput('cars2', label = "client side", choices = NULL)  
  )

shinyApp(ui = ui, server = server)

这是一个错误还是我错误地使用了小部件?我在Windows 7 64位上运行R版本3.1.3和Shiny 0.11.1。

3 个答案:

答案 0 :(得分:0)

我相信可以选择为选择设置初始化值。它位于options items。你必须传递一个你想要的值的数组,所以你应该能够传递它rownames(mtcars)它应该工作。虽然我从来没有尝试过多个价值,所以我并非100%正面。

所以我相信你案件中的命令是

selectizeInput('cars1', label= "server side", choices = NULL, options = list(items = rownames(mtcars))),

然后与其他线路一样。或者,如果这不起作用,请尝试对items数组中的初始值进行硬编码。我知道并不是很好,而且我不知道你有多少初始值,但我确信它会起作用。

答案 1 :(得分:0)

受到Colin的回答的启发,我找到了一个似乎有效的解决方法,尽管我仍然认为这实际上是一个闪亮的bug。首先通过添加参数selected = rowsnames(mtcars)来选择所有选择,然后再次调用updateSelectizeInput(),然后选择第一个项目,项目列表将填充所有选项/项目。这也适用于multiple = TRUE(因为我想使用它)。

library(shiny)

server <- function(input, output, session) {
  updateSelectizeInput(session, 'cars1', server = T, choices = rownames(mtcars), selected = rownames(mtcars))
  updateSelectizeInput(session, 'cars1', server = FALSE, selected = rownames(mtcars)[1])
  updateSelectizeInput(session, 'cars2', server = FALSE, choices = rownames(mtcars))
}

ui <- fluidPage(
   selectizeInput('cars1', label= "server side", choices = NULL, multiple = F),
  selectizeInput('cars2', label = "client side", choices = NULL)  
)

shinyApp(ui = ui, server = server)

答案 2 :(得分:0)

此问题在闪亮 v0.12.0(fixed)中已on CRAN now。服务器端selectizeInput()最初会加载前1000个选项(或n时加载第一个n < 1000选项。