如何解决如何更新在 moduleServer 中为在 moduleUI 中创建的 tabsetPanel 添加新的 tabPanel
我尝试在模块服务器中为在 moduleUI 中创建的 tabsetPanel 添加一个新的 tabPanel,但它不起作用。服务器非常清楚显示了哪个选项卡,但仍然无法向其添加新选项卡。我该如何解决这个问题?
在主 ui 和主服务器中,我创建了一个 tabsetPanel 和一个按钮来为其添加 newtab,它运行良好。但是当我尝试在一个模块中做同样的事情时,它就不再起作用了。
library(shiny)
## create a tabset Panel in module environment
tabModuleUI <- function(id,title){
ns = NS(id)
return(
tabPanel(title = title,value = id,shinydashboard::Box(width = NULL,tabsetPanel(id = ns("Product_module"),tabPanel(title = "Product 0",value = "Product 0"),tabPanel(title = "Product 1",value = "Product 1")),## button to add new tab
actionButton(inputId = ns("button"),label = "Click")))
)
}
tabModuleServer <- function(input,output,session,idModule){
ns <- session$ns
id_num = reactiveVal(1)
parentSession <- get("session",envir = parent.frame(1))
observeEvent(input$button,{
id_chr = paste("Product_module",id_num())
# server kNow which tab is shown
print(input[["Product_module"]])
# but cannot add a newtab in it
appendTab(inputId = "Product_module",tab = tabPanel(title = id_chr,value = id_chr,shinydashboard::Box()),session = session)
# even when i add a ns to inputId
appendTab(inputId = ns("Product_module"),session = session)
# but it can add a newtab in global environment
appendTab(inputId = "Product",session = parentSession)
id_num = id_num(id_num()+1)
})
}
## create a tabsetPanel in global environment
ui <- fluidPage(title="",tagList(tabsetPanel(
id = "Product",value = "Product 0"))),## button to add new tab
actionButton(inputId = "button",label = "Click"))
server <- function(input,session){
id_num = reactiveVal(1)
observeEvent(input[["button"]],{
id_chr = paste("Product",id_num())
# server kNow which tab is choosen
print(input[["Product"]])
# add add a newtab to it
appendTab(inputId = "Product",tab = tabModuleUI(id = id_chr,title = id_chr),select = T)
callModule(tabModuleServer,id = id_chr,idModule = id_chr)
id_num = id_num(id_num()+1)
})
}
shinyApp(ui,server)
解决方法
也许这就是您要找的。p>
library(shiny)
### create a tabset Panel in module environment
tabModuleUI <- function(id,title){
ns = NS(id)
return(
tabPanel(title = title,value = id,shinydashboard::box(width = NULL,tabsetPanel(id = ns("Product_module"),tabPanel(title = "Product_module 0",value = "Product_module 0") #,#tabPanel(title = "Product 1",value = "Product 1")
),## button to add new tab
actionButton(inputId = ns("button"),label = "Click Module")))
)
}
tabModuleServer <- function(input,output,session,idModule){
ns <- session$ns
id_num = reactiveVal(1)
parentSession <- get("session",envir = parent.frame(1))
observeEvent(input$button,{
id_chr = paste0("Product_module",id_num())
# server know which tab is shown
print(input[["Product_module"]])
print(input$button)
### but cannot add a newtab in it
appendTab(inputId = ns("Product_module"),tab = tabPanel(title = id_chr,value = id_chr,shinydashboard::box()),session = parentSession)
# ### even when i add a ns to inputId
# appendTab(inputId = ns("Product_module"),# shinydashboard::box()),session = session)
### but it can add a newtab in global environment
# appendTab(inputId = "Product",# shinydashboard::box()),session = parentSession)
id_num = id_num(id_num()+1)
})
}
## create a tabsetPanel in global environment
ui <- fluidPage(title="",tagList(tabsetPanel(
id = "Product",tabPanel(title = "Product 0",value = "Product 0"))),## button to add new tab
actionButton(inputId = "button",label = "Click Main")
)
server <- function(input,session){
id_numm = reactiveVal(1)
observeEvent(input[["button"]],{
id_chr = paste0("Product",id_numm())
# server know which tab is choosen
print(input[["Product"]])
# add add a newtab to it
appendTab(inputId = "Product",tab = tabModuleUI(id = id_chr,title = id_chr),select = T)
callModule(tabModuleServer,id = id_chr,idModule = id_chr)
id_numm = id_numm(id_numm()+1)
},ignoreInit = TRUE)
}
shinyApp(ui,server)
请注意,除非您保留以前的值,否则模块中的选项卡面板将重置。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。