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

时间:2017-12-09 13:42:38

标签: 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。

谢谢!

1 个答案:

答案 0 :(得分:0)

模块可以通过在服务器函数中返回它们来将响应式表达式传递给调用应用程序/模块。该文档提供了一些有关如何设置模块之间的交互和调用应用程序的示例 - 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)
相关问题