r - Shiny :根据选择更新 selectizeInput 选项

标签 r shiny

我正在尝试更新 choicesselectizeInput基于当前selected选择。这是我的尝试(导致循环):

library(shiny)
run_ui <- function() {

  ui <- selectizeInput('words', 'Search words:', choices = NULL, selected = NULL, multiple = TRUE, options = NULL)

  server <- function(input, output, session) {

    # change 'Search words' ----
    observeEvent(input$words, {

      # handle no words (reset everything)
      if (is.null(input$words)) {
        cowords <- letters

      } else {
        # update cowords (choices for selectizeInput)
        cowords <- unique(c(input$words, sample(letters, 5)))
      }

      # update UI
      print('updating')
      updateSelectizeInput(session, 'words', choices = cowords, selected = input$words, server = TRUE)

    }, ignoreNULL = FALSE)
  }
  runGadget(shinyApp(ui, server), viewer = browserViewer())
}

run_ui()

我怎样才能做到这一点?

最佳答案

如果你想坚持 server = TRUE ,这也许不是一个小问题。

一种可能的解决方法是到 debounce您正在观察的输入,然后检查并仅在发生更改时更新。这可能如下所示 - 我添加了一些 print声明,以便您可以更好地了解正在发生的事情。

library(shiny)

run_ui <- function() {

  ui <- selectizeInput('words', 'Search words:', choices = NULL, selected = NULL, multiple = TRUE, options = NULL)

  server <- function(input, output, session) {

    val <- "a"
    pasteCollPlus <- function(...) {
      paste(..., collapse = "+")
    }

    wordSelect <- debounce(reactive({input$words}), millis = 50)

    # change 'Search words' ----
    observeEvent(wordSelect(), {

      # handle no words (reset everything)
      if (is.null(input$words)) {
        cowords <- letters
      } else {
        # update cowords (choices for selectizeInput)
        cowords <- unique(c(input$words, sample(letters, 5)))
      }

      if (isTRUE(pasteCollPlus(val) == pasteCollPlus(input$words))) {
        print(paste("No update - val is", pasteCollPlus(val)))
      } else {
        # update UI
        print(paste("updating selection to", pasteCollPlus(input$words)))
        print(paste("val is", pasteCollPlus(val)))
        val <<- input$words
        updateSelectizeInput(session, 'words', choices = cowords, selected = input$words, server = TRUE)
      }

    }, ignoreNULL = FALSE)

  }
  runGadget(shinyApp(ui, server), viewer = browserViewer())
}

run_ui()

编辑

另一种解决方法是到 处理弹跳模式 明确地,为了阻止它。这可能更不优雅,但对于更多涉及/复杂的案例(应用程序)可能更健壮。一个例子如下:
library(shiny)
run_ui <- function() {

  ui <- selectizeInput('words', 'Search words:', choices = NULL, selected = NULL, multiple = TRUE, options = NULL)

  server <- function(input, output, session) {

    val <- "a"
    newVal <- NULL
    pasteCollPlus <- function(...) {
      paste(..., collapse = "+")
    }

    # change 'Search words' ----
    observeEvent(input$words, {

      # handle no words (reset everything)
      if (is.null(input$words)) {
        cowords <- letters
      } else {
        # update cowords (choices for selectizeInput)
        cowords <- unique(c(input$words, sample(letters, 5)))
      }

      if (isTRUE(pasteCollPlus(val) == pasteCollPlus(input$words))) {
        print(paste("No update - val is", pasteCollPlus(val)))
        val <<- newVal
      } else {
        # update UI
        print(paste("updating selection to", pasteCollPlus(input$words)))
        print(paste("val is", pasteCollPlus(val)))
        print(paste("newVal is", pasteCollPlus(newVal)))

        val <<- NULL
        newVal <<- input$words

        updateSelectizeInput(session, 'words', choices = cowords, selected = input$words, server = TRUE)
      }

    }, ignoreNULL = FALSE)

  }
  runGadget(shinyApp(ui, server), viewer = browserViewer())
}

run_ui()

关于r - Shiny :根据选择更新 selectizeInput 选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55078334/

相关文章:

r - 如何在 Shiny 中对多个表进行排序

ggplot 的 R Shiny Interactive 绘图标题

r - API 请求和curl::curl_fetch_memory(url, handle = handle) 中的错误:SSL 证书问题:证书已过期

r - 如果在 2 行中满足某些条件,如何在 R 数据框中添加新列以显示当前行和前一行中值的总和?

r - R中的子集字符向量

r - 强制将编码从未知设置为 UTF-8 或 R 中的任何编码?

R Shiny 交互式地选择变量来执行计算

R studio Shiny 的条件语句

html - Shiny - 将下拉菜单(选择标签)的大小(填充?)更改得更小

r - 在 R 中使用带有负值的 ggplot 的堆积条形图