r语言 - 为什么按钮的observeEvent被错误地触发(第二次)?



我写了一个显示模态对话框的小模块。用户使用对话框选择值,然后单击按钮进行确认。然后将这些值传递给首先调用该模块的应用程序。

当我第一次运行模块时,它按预期完成。第二次运行它时,自动单击模块中的确认按钮(同时,所选的值被重置)。当我第三次运行该模块时,它按预期完成了。第四次的时候,奇怪的行为还在继续。等等。

应用程序:

library(shiny)
source("condSelectModule2.R")
ui <- fluidPage(
condSelectUI("chooseCond"),
actionButton("makeChoice", "Choose"),
textOutput("showCond")
)
server <- function(input, output, session) {
observeEvent(input$makeChoice,  {
r <- condSelectServer("chooseCond", c(1,2,3))

output$showCond <- renderText(unlist(r()))             
})


}
shinyApp(ui, server)

模块:

condSelectUI <- function(id) {}
condSelectServer <- function(id, conds)
moduleServer(id, function(input, output, session) {

ns <- session$ns

showModal(modalDialog(
title="Select a number",
footer=tagList(     fluidRow(
selectInput(ns("Number"), "Select a number", choices=conds)
),
actionButton(ns("goBtn"), "Ok")
)))


observeEvent(input$goBtn,  {
print(c(input$Number))
removeModal()
}
)


# return value

reactive(input$Number)

})

选项ignoreInit = TRUE应该可以工作。试试这个

observeEvent(input$goBtn,  {
print(c(input$Number))
removeModal()
}, ignoreInit = TRUE)

最新更新