【问题标题】:R shiny selectizeInput not working with updateSelectizeInputR闪亮的selectizeInput不适用于updateSelectizeInput
【发布时间】:2022-02-24 11:15:33
【问题描述】:

我在下面分享了代码。 我有多个选项卡,其中一个选项卡有一个 selectizeInput,其中包含 50,000 个唯一值的列表。 因此,正如这里https://shiny.rstudio.com/articles/selectize.html 所建议的那样,我在服务器端使用它。 由于某些原因,包含 selectizeInput 元素的页面 Dashboard 2 没有反应。无论我在那里输入什么,该字段都是空的。 考虑到我非常大的闪亮应用程序,我的代码使用不同的 R 文件构建。 但是,为了复制问题,您只需要两个文件。第一个文件名为“app.R”,包含以下代码:

ui <- dashboardPage( 
  title = "Title test", 
  dashboardHeader(title = "Dashboard header"),
  
  dashboardSidebar(  
    includeCSS("www/styles.css"),
    
    sidebarMenu(
      
     menuItem('Retail1', tabName = "tab1", icon = icon("th"),
              menuItem('Dashboard2', tabName = 'retail_dashboard1')
              ),
      
      menuItem('Retail2', tabName = "tab2", icon = icon("th"),
               menuItem('Dashboard2', tabName = 'retail_dashboard2')
               )
      
    )
  ),
  
  
  
  dashboardBody( 
    
    tabItems(
      
      tabItem(tabName = "retail_dashboard2",
              uiOutput("ui_retail_dashboard2")              )
      
    )
  )
)


server <- function(input, output, session) {    
  
  source("Page_retail_dash2.R", local=T) 
  shiny::updateSelectizeInput(session=session, inputId ='element_with_list_of_cities', choices = rownames(mtcars), server = TRUE )
  
  
}
cat("\nLaunching   'shinyApp' ....")
shinyApp(ui, server)

第二个文件名为“Page_retail_dash2.R”,包含以下简单代码:

output$ui_retail_dashboard3 <- renderUI({ 
  
  tabsetPanel(type = "tabs",
              tabPanel("Dashboard 3",

                       
                       h3("Test"),
                       fluidRow(
                         column(2,
                                selectizeInput(inputId = "element_with_list_of_cities_dash3",
                                               label = "Cities",
                                               choices = NULL, 
                                               selected = NULL,
                                               multiple = TRUE # allow for multiple inputs
                                               ,options = list(create = FALSE, maxOptions = 1000)  # if TRUE, allows newly created inputs))
                                )) 
                       )
              )
  )
})

如果您只是复制并粘贴我的代码,您应该能够复制该问题。我还在这里附上我看到的我运行我的应用程序。 您可能会问为什么第一个选项卡是空的。在我的应用中,它不是空的,它有一些表,但你不需要它来复制这个问题。

【问题讨论】:

  • 对我来说还不清楚。您能否详细说明一下。
  • 当然,应该显示带有自动完成功能的值列表的框只是空的。

标签: r shiny


【解决方案1】:

那是因为你更新得太快了。您的变量是NULL,直到您转到该选项卡,然后您才应该更新。在sidebarMenu 中定义ID,然后在您位于该特定选项卡中时定义updateSelectizeInput。完整代码:

ui <- dashboardPage( 
  title = "Title test", 
  dashboardHeader(title = "Dashboard header"),
  
  dashboardSidebar(  
    #includeCSS("www/styles.css"),
    
    sidebarMenu(id="tabs",
      
      menuItem('Retail1', tabName = "tab1", icon = icon("th"),
               menuItem('Dashboard1', tabName = 'retail_dashboard1')
      ),
      
      menuItem('Retail2', tabName = "tab2", icon = icon("th"),
               menuItem('Dashboard2', tabName = 'retail_dashboard2')
      )
      
    )
  ),
  dashboardBody( 
    
    tabItems(
      
      tabItem(tabName = "retail_dashboard2",
              uiOutput("ui_retail_dashboard2")              )
      
    )
  )
)

server <- function(input, output, session) {    
  
  source("Page_retail_dash2.R", local=T)
  # observe({
  #   print(input$element_with_list_of_cities) 
  #   print(input$tabs)
  # })
  observeEvent(input$tabs,{
    if (input$tabs=="retail_dashboard2")  updateSelectizeInput(session=session, inputId ='element_with_list_of_cities', 
                         choices = rownames(mtcars) , selected=rownames(mtcars)[1], server = TRUE )
  })
  
}
#cat("\nLaunching   'shinyApp' ....")
shinyApp(ui, server)

请不要让您的 ID 匹配,我在 Page_retail_dash2.R 中放置了一个虚拟选择

output$ui_retail_dashboard2 <- renderUI({ 
  tabsetPanel(type = "tabs",
              tabPanel("My Dashboard",
                       h3("Test"),
                       fluidRow(
                         column(2,
                                selectizeInput(inputId = "element_with_list_of_cities",
                                               label = "Cities",
                                               choices = c("A","B"), 
                                               selected = "A",
                                               multiple = TRUE # allow for multiple inputs
                                               ,options = list(create = FALSE, maxOptions = 10000L)  # if TRUE, allows newly created inputs))
                                )) 
                       )
              )
  )
})

【讨论】:

  • 你真的让我很开心!如果你会在纽约告诉我,请给我喝酒!
  • 根据您的用例,您可能需要考虑使用updateTabItems。
  • 我住在东中城,只要你在附近就给我打电话!
  • 当然可以。今天早上从雨夹雪中挖掘......
【解决方案2】:
  1. tabItems 调用丢失。
  2. 要显示超过 1000 个选项,我们需要设置如下内容:selectizeInput(inputId = "myId", label = "myLabel", options = list(maxOptions = 100000L))

还可以查看我的相关答案here。

library(shiny)
library(shinydashboard)

ui <- dashboardPage( 
  title = "Dashboard", 
  dashboardHeader(title = "Dashboard"),
  dashboardSidebar(   
    # includeCSS("www/styles.css"),
    sidebarMenu(
      menuItem('Retail', tabName = "dash1", icon = icon("th"),
               menuItem('Dashboard2', tabName = 'retail_dashboard2'),
               menuItem('Dashboard3', tabName = 'retail_dashboard3'),
               menuItem('Dashboard4', tabName = 'retail_dashboard4'),
               menuItem('Dashboard5', tabName = 'retail_dashboard5'),
               menuItem('Dashboard6', tabName = 'retail_dashboard6'),
               menuItem('Dashboard7', tabName = 'retail_dashboard7'),
               menuItem('Dashboard8', tabName = 'retail_dashboard8'),
               menuItem('Dashboard9', tabName = 'retail_dashboard9'),
               menuItem('Dashboard10', tabName = 'retail_dashboard10'),
               menuItem('Dashboard11', tabName = 'retail_dashboard11'),
               menuItem('Dashboard12', tabName = 'retail_dashboard12')
      )
    )
  ),
  dashboardBody( 
    tabItems(
      tabItem(tabName = "retail_dashboard3",
              h3('Text'),
              fluidRow(column(12,
                              dataTableOutput("retail_dashboard3_table")))
      ),
      tabItem(tabName = "retail_dashboard4",
              h3('Text'),
              fluidRow(column(12,
                              dataTableOutput("retail_dashboard4_table")))
      ),
      tabItem(tabName = "retail_dashboard5",
              h3('Text'),
              fluidRow(column(12,
                              dataTableOutput("retail_dashboard5_table")))
      ),
      tabItem(tabName = "retail_dashboard6",
              h3('Text'),
              fluidRow(column(12,
                              dataTableOutput("retail_dashboard6_table")))
      ),
      tabItem(tabName = "retail_dashboard7",
              h3('Text'),
              fluidRow(column(12,
                              dataTableOutput("retail_dashboard7_table")))
      ),
      tabItem(tabName = "retail_dashboard8",
              h3('Text'),
              fluidRow(column(12,
                              dataTableOutput("retail_dashboard8_table")))
      ),
      tabItem(tabName = "retail_dashboard9",
              h3('Text'),
              fluidRow(column(12,
                              dataTableOutput("retail_dashboard9_table")))
      ),
      tabItem(tabName = "retail_dashboard10",
              h3('Text'),
              fluidRow(column(12,
                              dataTableOutput("retail_dashboard10_table")))
      ),
      tabItem(tabName = "retail_dashboard11",
              h3('Text'),
              fluidRow(column(12,
                              dataTableOutput("retail_dashboard11_table")))
      ),
      tabItem(tabName = "retail_dashboard12",
              h3('Text'),
              fluidRow(column(12,
                              dataTableOutput("retail_dashboard12_table")))
      ),
      tabItem(tabName = "retail_dashboard2",
              h3("Test"),
              fluidRow(
                column(2,
                       selectizeInput(inputId = "element_with_list_of_cities",
                                      label = "Cities",
                                      choices = NULL, 
                                      selected = NULL,
                                      multiple = TRUE, # allow for multiple inputs
                                      options = list(create = FALSE, maxOptions = 100000L)  # if TRUE, allows newly created inputs))
                       )) 
              )            
      )
    )
  )
)

server <- function(input, output, session) {    
  output$retail_dashboard3_table <- 
    output$retail_dashboard4_table <- 
    output$retail_dashboard5_table <- 
    output$retail_dashboard6_table <- 
    output$retail_dashboard7_table <- 
    output$retail_dashboard8_table <- 
    output$retail_dashboard9_table <- 
    output$retail_dashboard10_table <- 
    output$retail_dashboard11_table <- 
    output$retail_dashboard12_table <- renderDataTable({return(mtcars)})
  updateSelectizeInput(session=session, inputId ='element_with_list_of_cities', choices = 1:60000, server = TRUE)
}

cat("\nLaunching   'shinyApp' ....")
shinyApp(ui, server)

【讨论】:

  • 这是一个公平的观点,但不幸的是它并没有解决问题。我还会附上我看到的截图。我基本上看清了您在屏幕截图中分享的内容,但我这边没有显示下拉框。
  • @Angelo - 您的示例代码没有反映问题。请尝试创建一个可重现的示例。
  • @Angelo 问题可能是,当您尝试访问它们时尚未加载选项?
  • 但是怎么会呢?如您所见,我不是使用实际向量(具有 50k + 唯一值)而是仅使用 rownames(mtcars) 测试代码,所以我无法真正理解可能导致此问题的原因
  • @Angelo - 编辑后问题就很清楚了。问题是使用renderUI。它仅在您第一次访问选项卡后执行。此时updateSelectizeInput 已经尝试更新尚不存在的selectizeInput。我不会再次更新我的答案(它正在工作),因为@YBS 已经提供了关于renderUI 的解决方法。请参阅this 了解另一种解决方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 2017-01-05
  • 2021-06-27
  • 2019-01-24
  • 2018-12-22
相关资源
最近更新 更多