在 Shiny 模块中使用模态窗口 [英] using modal window in Shiny module

查看:33
本文介绍了在 Shiny 模块中使用模态窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Shiny 模块中使用模态窗口.用户与模态窗口交互,模块处理用户的输入.

I want to use a modal window inside a Shiny module. The user interacts with the modal window, the module processes the user's input.

在这个最小的示例中,模块应该在用户单击关闭模式"按钮时删除模式:

In this minimal example the module is supposed to remove the modal when the user clicks the "close modal" button:

library(shiny)

# Modal module UI
modalModuleUI <- function(id) {
  ns <- NS(id)
  actionButton(ns("openModalBtn"), "Open Modal")
}

# Modal module server
modalModule <- function(input, output, session) {

  myModal <- function() {
    modalDialog(
      actionButton("closeModalBtn", "Close Modal")
    )
  }
  # Show modal dialog on start up
  observeEvent(input$openModalBtn,
               ignoreNULL = FALSE,
               showModal(myModal())
               )

  # close modal on button click (not working)
  observeEvent(input$closeModalBtn, { 
    removeModal() 
  })
}

# Main app UI
ui <- fluidPage(modalModuleUI("foo"))

# Main app server
server <- function(input, output, session) {
  callModule(modalModule, "foo")
}

shinyApp(ui, server)

但是,点击关闭模式"按钮不会触发模块服务器函数中的observeEvent().我无法弄清楚如何访问(即观察)模块中模式窗口的内容.我猜这是一个命名空间问题.

However, clicking the "close modal" button does not trigger the observeEvent() in the module server function. I cannot figure out how to access (i.e. observe) the content of the modal window in the module. I guess it is a namespace issue.

交互式示例现在可以使用了.请参阅下面的答案.

The interactive example now works. See my answer below.

推荐答案

我自己重读了一遍 this 更仔细.与 renderUI 一样,模式中的 id 元素需要包装在 ns() 中,以使其在模块命名空间中可用.命名空间必须使用 ns <- session$ns 显式加载到模态中,如下所示:

I figured it out myself after re-reading this more carefully. Like with renderUI the id elements in the modal need to be wrapped in ns() to make them available in the module namespace. The namespace has to be loaded inside the modal explicitly using ns <- session$ns, like this:

library(shiny)

# Modal module UI
modalModuleUI <- function(id) {
  ns <- NS(id)
  actionButton(ns("openModalBtn"), "Open Modal")
}

# Modal module server
modalModule <- function(input, output, session) {

  myModal <- function() {
    ns <- session$ns
    modalDialog(actionButton(ns("closeModalBtn"), "Close Modal"))
  }

  # open modal on button click
  observeEvent(input$openModalBtn,
               ignoreNULL = FALSE,   # Show modal on start up
               showModal(myModal())
  )

  # close modal on button click
  observeEvent(input$closeModalBtn, { 
    removeModal() 
  })
}

# Main app UI
ui <- fluidPage(modalModuleUI("foo"))

# Main app server
server <- function(input, output, session) {
  callModule(modalModule, "foo")
}

shinyApp(ui, server)

注意:如果myModal函数定义在模块服务器函数之外,调用时必须通过session,即showModal(myModal(session))myModal <-function(会话){...}.

Note: If the myModal function is defined outside the module server function, one has to pass session when calling it, i.e. showModal(myModal(session)) and myModal <- function(session) {...}.

我已经更新了 示例应用,使其现在可以正常工作,并且还添加了一个 textInput.

I have updated the example app so that it works now and added a textInput too.

这篇关于在 Shiny 模块中使用模态窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆