renderUI 在嵌套闪亮模块内的模态中

renderUI in a modal inside nested shiny modules

假设我有我的 module1 下面显示了单击按钮时的模式。效果很好。

library(shiny)

module1_ui <- function(id) {
  shiny::tagList(
    actionButton(
      inputId = NS(namespace = id, id = "actbtn"), 
      label = "Click"
    )
  )
}

module1_server <- function(id, input_data) {
  stopifnot(is.reactive(input_data))
  
  moduleServer(
    id = id, 
    
    module = function(input, output, session) {
      # modal:
      modal_1 <- function() {
        modalDialog(
          title = "Modal Dialog", 
          footer = NULL, 
          easyClose = TRUE, 
          size = "l", 
          
          shiny::tagList(
            tags$h3(
              "I am a modal!"
            ), 
            
            uiOutput(outputId = NS(namespace = id, id = "rendered"))
          )
        )
      }
      
      output$rendered <- renderUI({
        shiny::tagList(
          tags$h1("And this is my rendered text")
        )
      })
      
      # show modal on btn click:
      observeEvent(input$actbtn, {
        showModal(
          modal_1()
        )
      })
    }
  )
}

ui <- fluidPage(
  module1_ui(id = "module1")
)

server <- function(input, output, session) {
  module1_server(
    id = "module1", 
    input_data = reactive({ })
  )
}

shinyApp(ui, server)

但是如果我将 module1 嵌套在另一个 module2 中,渲染的文本不会显示:

module2_ui <- function(id) {
  shiny::tagList(
    module1_ui(id = NS(namespace = id, id = "module1"))
  )
}

module2_server <- function(id, input_data) {
  stopifnot(is.reactive(input_data))
  
  moduleServer(
    id = id, 
    
    module = function(input, output, session) {
      module1_server(
        id = "module1", 
        input_data = input_data
      )
    }
  )
}

ui <- fluidPage(
  module2_ui(id = "module2")
)

server <- function(input, output, session) {
  module2_server(
    id = "module2", 
    input_data = reactive({ iris })
  )
}

shinyApp(ui, server)

我错过了什么?这可能与命名空间有关,但我无法弄清楚。

求助!

试试这个

library(shiny)

module1_ui <- function(id) {
  shiny::tagList(
    actionButton(
      inputId = NS(namespace = id, id = "actbtn"), 
      label = "Click"
    )
  )
}

module1_server <- function(id, input_data) {
  stopifnot(is.reactive(input_data))
  
  moduleServer(
    id = id, 
    
    module = function(input, output, session) {
      # modal:
      ns <- session$ns
      modal_1 <- function() {
        modalDialog(
          title = "Modal Dialog", 
          footer = NULL, 
          easyClose = TRUE, 
          size = "l", 
          
          shiny::tagList(
            tags$h3(
              "I am a modal!"
            ), 
            
            uiOutput(outputId = ns("rendered"))
          )
        )
      }
      
      output$rendered <- renderUI({
        shiny::tagList(
          tags$h1("And this is my rendered text")
        )
      })
      
      # show modal on btn click:
      observeEvent(input$actbtn, {
        showModal(
          modal_1()
        )
      })
    }
  )
}

module2_ui <- function(id) {
  ns <- NS(id)
  shiny::tagList(
    module1_ui(id = ns("module1"))
  )
}

module2_server <- function(id, input_data) {
  stopifnot(is.reactive(input_data))
  
  moduleServer(
    id = id, 
    
    module = function(input, output, session) {
      module1_server(
        id = "module1", 
        input_data = input_data
      )
    }
  )
}

ui <- fluidPage(
  module2_ui(id = "module2")
)

server <- function(input, output, session) {
  module2_server(
    id = "module2", 
    input_data = reactive({ iris })
  )
}

shinyApp(ui, server)