Module 闪亮:带有模块的insertTab

Module 闪亮:带有模块的insertTab,module,shiny,Module,Shiny,我试图在模块中调用insertTab()函数动态插入选项卡。由于某种原因,我的方法不起作用。我想问题在于如何将现有选项卡面板的id和值(应在其旁边添加选项卡)传递到模块 actionButUI = function(id, label=NULL) { ns = NS(id) tagList( actionButton(ns("button"), label = label) ) } actionBut = function(input, output, session,

我试图在模块中调用
insertTab()
函数动态插入选项卡。由于某种原因,我的方法不起作用。我想问题在于如何将现有
选项卡面板
id和
(应在其旁边添加
选项卡
)传递到模块

actionButUI = function(id, label=NULL) {
  ns = NS(id)
  tagList(
    actionButton(ns("button"), label = label)  
  )
}

actionBut = function(input, output, session, tabsetPanel_id, target) {
  observeEvent(input$button, {
    insertTab(
      inputId = tabsetPanel_id(), 
      tabPanel(
        "Dynamic", "This a dynamically-added tab"
      ),
      target = target
    )
  })
}




ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      actionButUI("append_tab", "Insert Tab")
    ),
    mainPanel(
      tabsetPanel(id = "tabs",
                  tabPanel("Hello", "This is the hello tab"),
                  tabPanel("Bar", "This is the bar tab")
      )
    )
  )
)
server <- function(input, output, session) {
  callModule(actionBut, "append_tab", reactive({input$tabs}), "Bar")
}

shinyApp(ui, server)
actionButUI=function(id,label=NULL){
ns=ns(id)
标签列表(
操作按钮(ns(“按钮”),标签=标签)
)
}
actionBut=函数(输入、输出、会话、tabsetPanel_id、目标){
ObserveeEvent(输入$按钮{
插入选项卡(
inputId=tabsetPanel_id(),
选项卡面板(
“动态”,“这是一个动态添加的选项卡”
),
目标=目标
)
})
}

ui名称空间似乎存在问题。下面的修改解决了这个问题

tabsetPanel(id = "append_tab-tabs",
            tabPanel("Hello", "This is the hello tab"),
            tabPanel("Bar", "This is the bar tab"))
insertTab
函数尝试在模块名称空间中添加ui元素,而不是全局元素。如果您查看
insertTab
的源代码,就会看到这行代码

inputId <- session$ns(inputId)

但是,如果使用嵌套模块,这种方法可能会变得非常混乱。

名称空间似乎存在问题。下面的修改解决了这个问题

tabsetPanel(id = "append_tab-tabs",
            tabPanel("Hello", "This is the hello tab"),
            tabPanel("Bar", "This is the bar tab"))
insertTab
函数尝试在模块名称空间中添加ui元素,而不是全局元素。如果您查看
insertTab
的源代码,就会看到这行代码

inputId <- session$ns(inputId)

但是,如果您使用嵌套模块,这种方法可能会变得非常混乱。

作为InsertTab函数的替代方法,您可以使用Ramnath解决方案

我把它做成了模块

library(shiny)

#---- Module Add dynamic tab ---
SidebarUi <- function(id) {
  ns <- NS(id)
  uiOutput(ns("sidebar"))
}

MainpanelUi <- function(id) {
  ns <- NS(id)
  uiOutput(ns("mainpanel"))
}
DynamicTabserver <- function(input, output, session) {
  ns <- session$ns
  output$sidebar <- renderUI({
    actionButton(ns("nTabs"), label = "Add tab")
  })
  output$mainpanel <- renderUI({
    uiOutput(ns('mytabs'))
  })

  output$mytabs <- renderUI({
    nTabs = input$nTabs
    myTabs = lapply(paste('Tab', 0:nTabs), tabPanel)
    do.call(tabsetPanel, myTabs)
  })

}

#---- App.R ---
ui = pageWithSidebar(headerPanel('Dynamic Tabs'),
                     sidebarPanel(SidebarUi("tabdemo")),
                     mainPanel(MainpanelUi("tabdemo")))
server = function(input, output, session) {
  callModule(DynamicTabserver, "tabdemo")
}
shinyApp(ui, server)
库(闪亮)
#----模块添加动态选项卡---

SidebarUi作为InsertTab函数的替代方法,您可以遵循Ramnath解决方案

我把它做成了模块

library(shiny)

#---- Module Add dynamic tab ---
SidebarUi <- function(id) {
  ns <- NS(id)
  uiOutput(ns("sidebar"))
}

MainpanelUi <- function(id) {
  ns <- NS(id)
  uiOutput(ns("mainpanel"))
}
DynamicTabserver <- function(input, output, session) {
  ns <- session$ns
  output$sidebar <- renderUI({
    actionButton(ns("nTabs"), label = "Add tab")
  })
  output$mainpanel <- renderUI({
    uiOutput(ns('mytabs'))
  })

  output$mytabs <- renderUI({
    nTabs = input$nTabs
    myTabs = lapply(paste('Tab', 0:nTabs), tabPanel)
    do.call(tabsetPanel, myTabs)
  })

}

#---- App.R ---
ui = pageWithSidebar(headerPanel('Dynamic Tabs'),
                     sidebarPanel(SidebarUi("tabdemo")),
                     mainPanel(MainpanelUi("tabdemo")))
server = function(input, output, session) {
  callModule(DynamicTabserver, "tabdemo")
}
shinyApp(ui, server)
库(闪亮)
#----模块添加动态选项卡---

SidebarUi您的代码中有一些语法问题,我在回答中没有提到。您应该将
“tabs”
作为
tabsetPanel\u id
参数传递,并作为模块中的
tabsetPanel\u id
访问它,而不是
tabsetPanel\u id()
您的代码中有一些语法问题我在回答中没有提到。您应该将
“tabs”
作为
tabsetPanel\u id
参数传递,并在模块中作为
tabsetPanel\u id
访问它,而不是
tabsetPanel\u id()
感谢您指出insertTab的名称空间问题和我的代码中的其他问题感谢您指出insertTab的名称空间问题和我的代码中的其他问题感谢您提供包装在模块中的Ramnath解决方案。然而,使用这种方法,我很难在每个选项卡中集成不同的ui元素,例如,提供模块包装的Ramnath解决方案的不同绘图方式。然而,使用这种方法,我很难在每个选项卡中集成不同的ui元素,例如不同的绘图