如何更新在 moduleServer 中为在 moduleUI 中创建的 tabsetPanel 添加新的 tabPanel
How to update add a new tabPanel in moduleServer for a tabsetPanel created in moduleUI
我尝试在模块服务器中为在 moduleUI 中创建的 tabsetPanel 添加一个新的 tabPanel,但它不起作用。服务器非常清楚显示了哪个选项卡,但仍然无法向其添加新选项卡。我该如何解决这个问题?
在主 ui 和主服务器中,我创建了一个 tabsetPanel 和一个为其添加新标签的按钮,并且效果很好。但是当我尝试在一个模块中做同样的事情时,它不再起作用了。
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"), tab = tabPanel(title = id_chr, value = id_chr,
shinydashboard::box()), session = session)
# but it can add a newtab in global environment
appendTab(inputId = "Product", tab = tabPanel(title = id_chr, value = id_chr,
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"))
server <- function(input, output, 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)
也许这就是您要找的。
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"), tab = tabPanel(title = id_chr, value = id_chr,
# shinydashboard::box()), session = session)
### but it can add a newtab in global environment
# appendTab(inputId = "Product", tab = tabPanel(title = id_chr, value = id_chr,
# 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, output, 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)
请注意,除非您保留以前的值,否则模块中的选项卡面板将重置。
我尝试在模块服务器中为在 moduleUI 中创建的 tabsetPanel 添加一个新的 tabPanel,但它不起作用。服务器非常清楚显示了哪个选项卡,但仍然无法向其添加新选项卡。我该如何解决这个问题?
在主 ui 和主服务器中,我创建了一个 tabsetPanel 和一个为其添加新标签的按钮,并且效果很好。但是当我尝试在一个模块中做同样的事情时,它不再起作用了。
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"), tab = tabPanel(title = id_chr, value = id_chr,
shinydashboard::box()), session = session)
# but it can add a newtab in global environment
appendTab(inputId = "Product", tab = tabPanel(title = id_chr, value = id_chr,
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"))
server <- function(input, output, 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)
也许这就是您要找的。
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"), tab = tabPanel(title = id_chr, value = id_chr,
# shinydashboard::box()), session = session)
### but it can add a newtab in global environment
# appendTab(inputId = "Product", tab = tabPanel(title = id_chr, value = id_chr,
# 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, output, 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)
请注意,除非您保留以前的值,否则模块中的选项卡面板将重置。