R shinyapp:renderUI 不适用于嵌套模块 R6 类

R shinyapp: renderUI does not work with nested modules R6 classes

我有一个带有模块的闪亮大应用程序,我正在 R6 classes 中转换其中的一些。除了嵌套模块外,一切正常:我遇到了命名空间问题,并且 uiOutput 不起作用。

这是一个可重现的例子。 class ClassTest 是 classes 层次结构的最后一个 class。它由 App 直接调用或通过 Temp class 调用。在后一种情况下(嵌套 class),uiOutput 中包含的元素不起作用。

library(shiny); library(R6)

ClassTest = R6Class(
  "ClassTest",
  public = list(
    # attributes
    id = NULL,

# initialize
initialize = function(id){
  self$id = id
},
# UI
ui = function(){
  ns = NS(self$id)
  tagList(
    h4('Test class'),
    uiOutput(ns('showText'))
  )
},

# server
server = function(id){
  moduleServer(id,
               function(input, output, session){
  ns = session$ns

  output$showText <- renderUI({ 
    print('In showText <- renderUI')
    tagList(
      p('I am the showText renderUI of the Test class')
      )
    })
}
)
  },

call = function(input, ouput, session){
  self$server(self$id)
}
     )
    )

#----------------------------------------------------------


Temp = R6Class(
  "Temp",
  public = list(
    # attributes
    temp = NULL,
    id = NULL,

# initialize
initialize = function(id){
  self$id = id
  self$temp <- ClassTest$new('testTiers')
},
# UI
ui = function(){
  ns = NS(self$id)
  tagList(
    uiOutput(ns('showTestClass'))
  )
},

# server
server = function(id){
  moduleServer(id,
               function(input, output, session){
                 ns = session$ns 
   output$showTestClass <- renderUI({ 
    self$temp$ui()
    })
})
  },

call = function(input, ouput, session){
  self$server(self$id)
}
  )
)

#----------------------------------------------------------


App = R6Class(
  "App",
  public = list(
    # attributes
    temp = NULL,
    classT = NULL,

# initialize
initialize = function(){
  self$temp = Temp$new('temp')
  self$classT = ClassTest$new('directTest')
  
},
# UI
ui = function(){
  tagList(
    h3('Call by another class'),
    self$temp$ui(),
    hr(),
    h3('Direct call'),
    self$classT$ui()
  )
},

# server
server = function(input, output, session){
  self$temp$call()
  self$classT$call()
    }
  )
)

app = App$new()

shiny::shinyApp(app$ui(), app$server)

你的代码有点复杂,但我敢肯定你错过了调用母亲class中childclass的server和child 模块也需要尊重 mothers 命名空间。这是一个工作示例,它按预期执行:

library(shiny)
library(R6)

MyModule <- R6Class(
   "MyModule",
   public = list(id = NULL,
                 initialize = function(id) {
                    self$id <- id
                 }, 
                 ui = function() {
                    ns <- NS(self$id)
                    tagList(h4(self$id), 
                            actionButton(ns("do"), "Calc!"), 
                            verbatimTextOutput(ns("print")))
                 },
                 
                 server = function() {
                    moduleServer(self$id, function(input, output, session) {
                       output$print <- renderPrint({
                          input$do
                          sample(100, 1)
                       })
                    })
                 }
   )
)

MyMotherModule <- R6Class(
   "MyMotherModule",
   public = list(id = NULL,
                 child = NULL,
                 initialize = function(id) {
                    self$id <- id
                    self$child <- MyModule$new(NS(id)("child"))
                 },
                 ui = function() {
                    self$child$ui()
                 },
                 server = function() {
                    self$child$server()
                 }
   )
)

App <- R6Class(
   "App",
   public = list(child1 = NULL,
                 child2 = NULL,
                 mother = NULL,
                 initialize = function() {
                    self$child1 <- MyModule$new("child1")
                    self$child2 <- MyModule$new("child2")
                    self$mother <- MyMotherModule$new("mother1")
                 },
                 ui = function() {
                    fluidPage(
                       fluidRow(
                          self$child1$ui(),
                          self$child2$ui(),
                          self$mother$ui()
                       )
                    )
                 },
                 server = function() {
                    function(input, output, session) {
                       self$child1$server()
                       self$child2$server()
                       self$mother$server()
                    }
                 }
   )
)

app <- App$new()

shinyApp(app$ui(), app$server())

一些备注

  1. 母模块中 child 模块的 id 也必须命名空间 MyModule$new(NS(id)("child")) 以符合名称必须仅在模块内而不是整个模块内唯一的想法.如果我在我的示例中没有命名空间 child,那么顶层的 child 元素会弄乱它。 (从技术上讲,它可以 w/o 命名空间,但是如果您碰巧使用带有 child 元素名称的顶级元素,您的用户会得到奇怪的结果。
  2. 只有在调用相应的服务器函数时,模块才会工作(即服务器逻辑启动)。因此,您需要在您的母模块中添加 self$child$server() 以“打开”child 的服务器逻辑。