通过单击多个 div 打开 Shiny Modal 对话框

Open Shiny Modal dialog box by clicking on multiple divs

我有以下Shiny-app-

library(shiny)

  shinyApp(
    ui = fluidPage(
      useShinyjs(),  # Set up shinyjs
      div(id = "Div1", style = "height: 100px; width: 100px; background-color: red;"),
      div(id = "Div2", style = "height: 100px; width: 100px; background-color: blue;")
    ),
    server = function(input, output) {
      onclick('Div1', showModal(modalDialog(
            div(id = "Div3", style = "height: 100px; width: 100px; background-color: black;", tableOutput("tab")),
          )))
    }
  )

在此应用程序中,如果我单击 Div1,则会打开 Modal dialog box。但是我想扩展它的条件是,如果我单击 Div1Div2 中的任何一个,那么相同的 dialog box 将打开。

有什么方法可以实现吗?

您可以根据 https://github.com/daattali/shinyjs/issues/167

使用解决方法
library(shiny)
library(shinyjs)

shinyApp(
    ui = fluidPage(
        useShinyjs(),  # Set up shinyjs
        div(id = "Div1", style = "height: 100px; width: 100px; background-color: red;"),
        div(id = "Div2", style = "height: 100px; width: 100px; background-color: blue;")
    ),
    server = function(input, output, session) {

        ids <- c("Div1", "Div2")
        for (id in ids) {
            local({
                shinyjs::onclick(id, {
                    showModal(modalDialog(
                        div(id = "Div3", style = "height: 100px; width: 100px; background-color: black;", tableOutput("tab")),
                    ))
                })
            })
        }
    }
)