R语言 调用模块进入另一个模块

c3frrgcw  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(116)

我想在点击按钮时显示一个框架。我为按钮创建一个mod,为框架创建一个mod。在 mod_button_server 函数中,我调用 mod_frame_server,在那里我创建框架并将其分配给输出

mod_button.R

mod_button_ui <- function(id){ ns <- NS(id); tagList(
    actionButton(ns("button"), id) )}

mod_button_server <- function(id){ moduleServer(id, function(input, output, session){ns <- session$ns
    observeEvent(input$button, {
      print(id)
      mod_dataframe_server("table")
    })})}

字符串

  • mod_frame.R**
mod_dataframe_ui <- function(id){ ns <- NS(id); tagList(
    tableOutput(ns("mytable")))}

mod_dataframe_server <- function(id){moduleServer(id, function(input, output,session){ ns <- session$ns;
    df <- data.frame(Years = seq(2000, 2009), Colonne2 = rnorm(10), Colonne3 = rnorm(10), Colonne4 = rnorm(10))
    output$mytable <- renderTable({df})})}

app_ui.R

app_ui <- function(request) {tagList(golem_add_external_resources(),
    mod_button_ui("B1"), #mod_button_ui("B2"),
    mod_dataframe_ui("table"),
  )}

app_server.R

app_server <- function(input, output, session) {
  mod_button_server("B1") #mod_button_server("B2")}

apeeds0o

apeeds0o1#

选项1

您可以从mod_dataframe_server输出一个 Dataframe ,如下所示:

mod_dataframe_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    df <- data.frame(Years = seq(2000, 2009),
                     Colonne2 = rnorm(10),
                     Colonne3 = rnorm(10),
                     Colonne4 = rnorm(10))
    return(df)
  })
}

字符串
然后可以在mod_button_server中访问,如下所示:

mod_button_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    observeEvent(input$button, {
      df1 <- mod_dataframe_server(ns("mytable"))
      output$table <- renderTable(df1)
    })
  })
}


mod_dataframe_ui可以作为默认的Golem模板,因为它不需要被调用。如果表发生在mod_button_ui中:

mod_button_ui <- function(id){
  ns <- NS(id)
  tagList(
    actionButton(ns("button"), label = "Click"),
    tableOutput(ns("table"))
  )
}


应用服务器函数与您尝试的相同,app_ui看起来像这样:

app_ui <- function(request) {
  tagList(
    golem_add_external_resources(),
    fluidPage(
      mod_button_ui("B1"),
    )
  )
}

选项二

或者,您可以只使用按钮模块并删除框架模块。这意味着mod_button_server将在按下按钮时使用observe()bindEvent()来创建和刷新表。mod_button_uiapp_uiapp_server与第一个选项相同。

mod_button_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns

    observe({
      output$table <- renderTable(
        data.frame(Years = seq(2000, 2009),
                   Colonne2 = rnorm(10),
                   Colonne3 = rnorm(10),
                   Colonne4 = rnorm(10))
      )
    }) |> bindEvent(input$button)
  })
}

工作示例

这实现了上面的选项2。选项1也包括在内,但我已经注解掉了。

library(shiny)

mod_dataframe_server <- function(id){
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    df <- data.frame(Years = seq(2000, 2009),
                     Colonne2 = rnorm(10),
                     Colonne3 = rnorm(10),
                     Colonne4 = rnorm(10))
    return(df)
  })
}

mod_button_server <- function(id){
  moduleServer(id, function(input, output, session) {
    ns <- session$ns
    
    # option 1
    # observeEvent(input$button, {
    #   df1 <- mod_dataframe_server(ns("mytable"))
    #   output$table <- renderTable(df1)
    # })

    # option 2
    observe({
      output$table <- renderTable(
        data.frame(Years = seq(2000, 2009),
                   Colonne2 = rnorm(10),
                   Colonne3 = rnorm(10),
                   Colonne4 = rnorm(10))
      )
    }) |> bindEvent(input$button)
  })
}

mod_button_ui <- function(id){
  ns <- NS(id)
  tagList(
    actionButton(ns("button"), label = "Click"),
    tableOutput(ns("table"))
  )
}

app_ui <- function(request) {
  tagList(
    fluidPage(
      mod_button_ui("B1"),
    )
  )
}

app_server <- function(input, output, session) {
  mod_button_server("B1") 
}

shinyApp(
  ui = app_ui,
  server = app_server
)

相关问题