R 观察模式(轻松)关闭时的光泽

R 观察模式(轻松)关闭时的光泽,r,shiny,modal-dialog,R,Shiny,Modal Dialog,我正在寻找一种方法,当easy close选项为TRUE时,根据闪亮模式的关闭触发一个事件(因此在模式外单击将关闭它)。由于没有链接到模态的ID,我似乎无法捕获此事件。我尝试在“观察”事件中包装模态,但这只会触发模态的打开,而不会关闭 任何想法。。。 谢谢 示例:如果此模式通过在其外部单击而关闭,则我希望触发一个事件,而不是“取消”按钮。下面的代码仅在打开时触发 library(shiny) ui <- fluidPage( fluidRow( actionButton(in

我正在寻找一种方法,当easy close选项为TRUE时,根据闪亮模式的关闭触发一个事件(因此在模式外单击将关闭它)。由于没有链接到模态的ID,我似乎无法捕获此事件。我尝试在“观察”事件中包装模态,但这只会触发模态的打开,而不会关闭

任何想法。。。 谢谢


示例:如果此模式通过在其外部单击而关闭,则我希望触发一个事件,而不是“取消”按钮。下面的代码仅在打开时触发

library(shiny)
ui <- fluidPage(
  fluidRow(
    actionButton(inputId = "enterText", label = "Enter name", align = "left"),
    h1(textOutput("myOutput"))
  )
)

server <- function(input, output, session) {
  myText <- reactiveValues(input = "...")
  myModal = modalDialog(h3("Enter a string, then click outside this modal to close and display the text"),
                        textInput(inputId = "myString", label = "Enter a string: "),
                        title = "Input", easyClose = TRUE, footer = modalButton("Dismiss"))
  test = observe(myModal)

  #Open the modal when button clicked
  observeEvent(input$enterText,{
    showModal(myModal)
    })

  #Observe the modal, should fire when it CLOSES by clicking outside the modal (easy-close)
  observeEvent(test, {
    myText$input = input$myString
    print("observed")
  }, ignoreInit = T)

  output$myOutput = renderText(myText$input)

}

shinyApp(ui = ui, server = server)
库(闪亮)

ui我想为任何不需要此选项的人提供一个更简单的答案,以便使用easyClose=TRUE,就像OP一样——在这种情况下,这是一个比SBista在评论中的链接更简单的解决方案

这只是用您自己的操作按钮替换了模态的默认“dismise”按钮,这允许您在关闭模态时执行一些额外的操作。这是通过
footer=
参数设置的

在本例中,模态包含复选框,在模态关闭之前,复选框不会生效

library(shiny)
shinyApp(
  ui <- fluidPage(
    fluidRow(
      ## Button to display modal:
      actionButton(inputId = "display_modal",label = "Display modal"),
      ## Print the choices that were made in the modal:
      h1(textOutput("checked_letters"))
    )
  ),

  server <- function(input, output) {
    ## These values allow the actions made in the modal to be delayed until the
    #  modal is closed
    values = reactiveValues(to_print = "",   ## This is the text that will be displayed
                            modal_closed=F)  ## This prevents the values$to_print output from 
                                             #  updating until the modal is closed

    ## Open the modal when button clicked
    observeEvent(input$display_modal,{
      values$modal_closed <- F
      showModal(modalDialog(
        checkboxGroupInput("checkboxes",label = "Select letters",
                           choices = LETTERS[1:7]),
        ## This footer replaces the default "Dismiss" button,
        #  which is 'footer = modalButton("Dismiss")'
        footer = actionButton("dismiss_modal",label = "Dismiss")
        ))
    })

    ## This event is triggered by the actionButton inside the modalDialog
    #  It closes the modal, and by setting values$modal_closed <- T, it
    #  triggers values$to_print to update.
    observeEvent(input$dismiss_modal,{
      values$modal_closed <- T
      removeModal()
    })
    ## values$to_print is only updated once the modal is closed.
    observe({
      if(values$modal_closed){
        values$to_print <- paste(input$checkboxes)
      }
    })
    ## Forward the values$to_print to the UI
    output$checked_letters = renderText({values$to_print})
  }
)
库(闪亮)
shinyApp(

ui与第一个答案类似,我的方法只适用于
easyClose=FALSE
。我们可以创建一个自定义函数
modalActionButton
,它基本上是从
actionButton
复制粘贴,而不是将
actionButton
observeEvent
一起使用“模态”

我改编了下面第一个答案中的示例:

library(shiny)

# this is basically copied from actionButton() and just "`data-dismiss` = "modal" 
# from modalButton() is added:
modalActionButton <- function(inputId, label, icon = NULL, width = NULL, ...) {
  
  value <- restoreInput(id = inputId, default = NULL)
  tags$button(id = inputId, type = "button", style = if (!is.null(width)) 
    paste0("width: ", validateCssUnit(width), ";"), type = "button", 
    class = "btn btn-default action-button", `data-dismiss` = "modal", `data-val` = value, 
    list(shiny:::validateIcon(icon), label), ...)
  
}

shinyApp(

    ui <- fluidPage(
    fluidRow(
      ## Button to display modal:
      actionButton(inputId = "display_modal",label = "Display modal"),
      
      ## Print the choices that were made in the modal:
      h1(textOutput("checked_letters"))
    )
  ),
  
  server <- function(input, output) {
    
    values = reactiveValues(to_print = "")   ## This is the text that will be displayed

    observeEvent(input$display_modal, {
      showModal(modalDialog(
        checkboxGroupInput("checkboxes",label = "Select letters",
                           choices = LETTERS[1:7]),

        title = "This is a modal page with a 'modalActionButton' ",
        size = "l",
        easyClose = FALSE,
        # here is the modalActionButton
        footer = modalActionButton("close",
                                   "Close")
      ))
    })

    observeEvent(input$close, {
        values$to_print <- input$checkboxes
      })

    ## Forward the values$to_print to the UI
    output$checked_letters <- renderText({values$to_print})

   }
)
库(闪亮)
#这基本上是从actionButton()复制而来的,只是“`data Disclose`=”model”
#从modalButton()添加:

modalActionButton您可以有一个
observeEvent
打开模式并触发此事件。它将在用户关闭模式之前触发此其他事件。但是如果此事件很慢,则可能会让用户感到恼火,因为在其他过程结束之前,他将无法关闭模式…谢谢,但我不会真的取消理解你的意思。我已经更新了上面的代码示例,但这只会触发模态的打开而不是关闭。请提供一个最小的示例嗨,谢谢你的回复。上面的代码是我能想到的最小示例。虽然它在这一点上没有做它应该做的事情:当模态打开时,它会打印“观察到的”,但我想要使用“轻松关闭”(在模式外部单击以关闭)关闭时发生的操作。也许链接会对你有所帮助。这确实是一种只在模式关闭后更新模式的方法。实际上,我现在正在使用这种方法,因为它是一个非常简单的实现。不幸的是,我希望“轻松关闭”是真实的,并具有与使用按钮相同的效果,因此这篇文章就是完整的。