首页 > 解决方案 > updateSelectInput 调用后如何重新分配输入?

问题描述

我有以下服务器代码,它返回错误“尝试在 get1index 中选择少于一个元素”

server <- function(input, output, session) {
  observeEvent(input$search, {
  ....
  enable("show_term")
  updateSelectInput(session, "show_term",
                    choices = list_of_terms, selected = list_of_terms[1])
  
  unique_words = unique(extended.dictionary[[which(list_of_terms == input$show_term)]][,c("word", "abbr")])
  
  ...
  })}
  
  

错误是因为input$show_termequal""中没有这样的元素而引起的list_of_term。这意味着updateSelectInput不会为 重新分配值input$show_term

然而,当我把它放在unique_words下面output$raw_dic = renderTable(...)时它工作得很好。

server <- function(input, output, session) {
  observeEvent(input$search, {
  ....
  enable("show_term")
  updateSelectInput(session, "show_term",
                    choices = list_of_terms, selected = list_of_terms[1])

  output$raw_dictionary = renderTable(colnames = FALSE, striped = TRUE,
     {if (!(collected.data[[which(list_of_terms == input$show_term)]] %in%
                                                               c("No papers", "No matches"))){
      unique_words = unique(extended.dictionary[[which(list_of_terms == input$show_term)]][,c("word", "abbr")])
      collected.chuncks = collected.data[[which(list_of_terms == input$show_term)]]}})
  
  ...
  })}

因此,在评估的第二种情况下,which(list_of_terms == input$show_term)存在input$show_term等于list_of_terms[1]

这里我不明白重新分配过程背后的逻辑。谁能给我解释一下?

更新:闪亮应用程序的完整代码存放在 GitHub(问题部分在 app.R 第 342-355 行):https ://github.com/Dobrokhotov1989/Abbrevimate

标签: rshiny

解决方案


推荐阅读