【发布时间】:2020-10-17 19:00:48
【问题描述】:
我有一个带有模块的大型闪亮应用程序,我正在将其中一些模块转换为 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)
【问题讨论】: