【问题标题】:Why is an observeEvent of a button is triggered falsely (the second time)?为什么按钮的observeEvent被错误触发(第二次)?
【发布时间】:2021-08-31 11:02:13
【问题描述】:

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

当我第一次运行模块时,它会按需要完成。我第二次运行它时,模块内的确认按钮会自动被点击(同样,选择的值也会被重置)。 当我第三次运行该模块时,它会按需要完成。第四次,奇怪的行为仍在继续。以此类推。

下面的最小reprex。

应用程序:

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)
    
  })

【问题讨论】:

  • 我的感觉是我调用模块的方式有缺陷。
  • condSelectUI在哪里?
  • @PorkChop 已添加,谢谢。这是一个空函数。

标签: r shiny shiny-reactivity


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    相关资源
    最近更新 更多