【发布时间】:2018-08-03 07:14:51
【问题描述】:
从 Shiny 模块外部使用 update* 而不重复 callModule() 调用或参数的最佳方法是什么?
小例子:
library(shiny)
numericInputModUI <- function (id) {
ns <- NS(id)
tagList(
numericInput(ns("val"), "Value", value = 0),
textOutput(ns("text"))
)
}
numericInputMod <- function (input, output, session,
updateVal = NA, displayText = "default text") {
output$text <- renderText(displayText)
if (!is.na(updateVal)) updateNumericInput(session, "val", value = updateVal)
}
ui = fluidPage(
numericInputModUI("module"),
actionButton("updateBad", label = "Bad Update"),
actionButton("updateBetter", label = "Better Update")
)
server = function(input, output, session) {
callModule(numericInputMod, "module", displayText = "original text")
observeEvent(
input$updateBad,
callModule(numericInputMod, "module", updateVal = 1)
)
observeEvent(
input$updateBetter,
callModule(numericInputMod, "module", updateVal = 2, displayText = "original text")
)
}
shinyApp(ui, server)
错误更新使用默认值覆盖原始文本。更好的更新通过重新传递原始文本来避免这种情况,但这并不理想,因为:
- 至少需要两次 callModule() 调用。
- 您必须重复 callModule() 参数。
理想情况下,一个 callModule() 调用将负责指定模块参数和更新行为。不过,我还没有找到或想出这样做的方法。
【问题讨论】: