让模块的父服务器知道模块内发生了一些事情

  • 本文关键字:模块 发生了 服务器 shiny
  • 更新时间 :
  • 英文 :


我正在构建一个显示数据表并允许您添加数据的应用程序。数据的添加是通过表单构建的。此表格由模块编写。我想发生的事情是,可以填写表单,按"添加"按钮,并更新表中的数据。

举例来说,您能帮我弄清楚如何完成以下代码:

library(shiny)
library(shinydashboard)
moduleInput <- function(id){
  ns <- NS(id)
  sidebarPanel(
    actionButton(ns("action1"), label = "click")
  )
}
module <- function(input, output, session){
  observeEvent(input$action1, {
    # Do stuff here,
    # -> let the parent module or server know that something has happened
  })
}
ui <- fluidPage(
  verbatimTextOutput("module.pressed"),
  moduleInput("first")
  )

server <- function(input, output, session){
  # print the currently open tab
  output$module.pressed <- renderPrint({
    #-> Write that we have pressed the button of the module
  })
  callModule(module,"first")
}
shinyApp(ui = ui, server = server)

我所想做的就是找到一种简单的方法来在输出字段中module.pressed中出现true。

谢谢!

模块可以通过在其服务器函数中返回反应性表达式来调用应用程序/模块。该文档提供了一些有关如何在模块和调用应用之间建立交互的示例-https://shiny.rstudio.com/articles/modules.html

如果模块需要使用反应性表达式,则以反应性表达为函数参数。如果模块想将反应性表达式返回到调用应用中,请返回功能中的反应性表达式列表。

library(shiny)
moduleInput <- function(id){
  ns <- NS(id)
  sidebarPanel(
    actionButton(ns("action1"), label = "click")
  )
}
module <- function(input, output, session){
  action1 <- reactive(input$action1)
  return(reactive(input$action1))
}
ui <- fluidPage(
  verbatimTextOutput("module.pressed"),
  moduleInput("first")
)
server <- function(input, output, session){
  action1 <- callModule(module,"first")
  output$module.pressed <- renderPrint({
    print(action1())
  })
}
shinyApp(ui = ui, server = server)

最新更新