r - 更新闪亮中相同单选按钮的选择参数
问题描述
我想从 R/shiny 中的 radioButtons-widget 更新选择参数。当用户选择一个选项时,choices 参数应该根据用户的第一个选项进行更新。我用样本函数用 4 个随机字母模拟了这个。看来更新并没有停止,而且更新了好几次。如何防止多次更新的行为?
这是重现我的方法的代码:
library("shiny")
ui <- fluidPage(
radioButtons("answerchoice", label = "item", choices = sample(letters, 4), selected = NULL,
)
)
server <- function(input, output, session) {
observeEvent(input$answerchoice,{
updateRadioButtons(
session = session,
inputId = "answerchoice",
choices = sample(letters, 4)
)
})
}
shinyApp(ui = ui, server = server)
解决方案
似乎默认设置selected = NULL
可能是问题所在。radioButton
最初选择一个值。这可能会导致多次更新。通过设置selected
为空。该应用程序不会不受控制地更新。
library("shiny")
ui <- fluidPage(
radioButtons("answerchoice",
label = "item",
choices = sample(letters, 4),
selected = character(0)
)
)
server <- function(input, output, session) {
observeEvent(input$answerchoice,{
updateRadioButtons(
session = session,
inputId = "answerchoice",
choices = sample(letters, 4),
selected = character(0)
)
})
}
shinyApp(ui = ui, server = server)
推荐阅读
- kubernetes - k0s kubectl exec 和 kubectl 端口转发被破坏
- python - 基于 Python Pandas 中的比较,比较 DataFrame 列并将另外两列添加到 DataFrame
- solr - 无法在 solr 4.10 中恢复备份
- sql - 使用多个联接时如何正确使用 COUNT()?
- sql - 在动态 SQL 中,未创建表
- asp.net-core - 在组件文档中添加TagHelper
- firebase - 从 Firestore 的集合中获取多个特定数据的最佳方法是什么?
- html - iFrame 不显示 PDF 页面
- performance - 如何使用 pyspark 提高 csv 到 parquet 文件格式的性能?
- django - 在 django 中使用 scss:scss 在 CACHE “应用”中编译成 css,但默认情况下“覆盖”引导主题