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

塞缪尔·维佐雷克(Samuel Wieczorek)

我有一个带有模块的大型闪亮应用程序,并且正在R6类中转换其中一些模块。除嵌套模块外,其他所有方法都运行良好:我遇到名称空间问题,并且uiOutput无法正常工作。

这是一个可复制的示例。类ClassTest是类层次结构中的最后一个类。它可以直接由App调用,也可以通过Temp类调用。在后一种情况(嵌套类)中,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)
杀人的

您的代码有点复杂,但是我确定您会错过server在父类中调用子类的子级的操作,并且子模块也需要尊重母亲的命名空间。这是一个工作示例,它可以完成预期的工作:

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. id母模块中的子模块也必须命名空间MyModule$new(NS(id)("child"))符合的想法,一个名称必须是唯一的模块中是唯一的,而不是整体。如果child在我的示例中没有命名空间,则child顶级元素将使其混乱。(从技术上讲,它不需要命名空间,但如果您碰巧使用带有子元素名称的顶级元素,则用户将得到奇怪的结果)。
  2. 仅当调用相应的服务器功能时,模块才起作用(即服务器逻辑生效)。因此,您需要添加self$child$server()您的母亲模块,以“顺次”孩子的服务器逻辑。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章