R Shiny : Change tabs from within a module

标签 r shiny namespaces scoping shinymodules

我有一个带有多个选项卡的 Shiny 应用程序,我希望选项卡内有操作按钮,允许用户切换选项卡。我找到了以下页面:https://www.titanwolf.org/Network/q/e6b187b8-6cad-4ece-ad16-7ec73ed2f758/y How to switch between shiny tab panels from inside a module? ,这似乎表明问题是范围/命名空间错误,但它们没有完全解释正在发生的事情,而且我没有足够的声誉点来评论其他要求澄清的 stackoverflow 帖子。

这是我的示例代码:

modtab1_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 1',
           value = NS(id, 'tab.1'),
           
           h4('This is the first tab'),
           actionButton(NS(id, 'nexttab'), 'Next Tab')
           
          )
}

modtab1_server <- function(id) {
  moduleServer(id,
               function(input, output, session) {
                 observeEvent(input$nexttab, {
                   updateTabsetPanel(session = session, inputId = NS(id, 'tabs'), selected = NS(id, 'tab.2'))
                   print('button clicked')
                 })
               })
}

modtab2_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 2',
           value = NS(id, 'tab.2'),
           
           h4('This is the second tab'),
          )
}


ui <- fluidPage(
  tabsetPanel(
    id = 'tabs',
    modtab1_ui('tab1'),
    modtab2_ui('tab1')
  )
)

server <- function(input, output, session) {
  modtab1_server('tab1')
}

shinyApp(ui = ui, server = server)

编辑以解决新问题

modtab1_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 1',
           value = NS(id, 'tab.1'),

           h4('This is the first tab'),
           actionButton(NS(id, 'nexttab'), 'Next Tab'),

           textInput(NS(id,'userid'), 'User ID'),
           textOutput(NS(id, 'useridout'))
          )
}

modtab1_server <- function(id) {
  moduleServer(id,
               function(input, output, session) {
                 observeEvent(input$nexttab, {
                   updateTabsetPanel(session = session, inputId =  'tabs', selected = NS('tab2', 'tab.2'))
                   print('button clicked'),
                 })
                 
                 output$useridout <- renderText(input$userid)
               })
}

modtab2_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 2',
           value = NS(id, 'tab.2'),

           h4('This is the second tab'),
           actionButton(NS(id, 'firsttab'), 'First Tab'),

           textInput(NS(id, 'userid'), 'User ID'),
           textOutput(NS(id, 'useridout'))
          )
}

modtab2_server <- function(id) {
  moduleServer(id,
               function(input, output, session) {
                 observeEvent(input$firsttab, {
                   updateTabsetPanel(session = session, inputId =  'tabs', selected = NS('tab1', 'tab.1'))
                   print('button clicked'),
                 })
                 
                 output$useridout <- renderText(input$userid)
               })
}


ui <- fluidPage(
  tabsetPanel(
    id = 'tab1-tabs',
    modtab1_ui('tab1'),
    modtab2_ui('tab2')
  )
)

server <- function(input, output, session) {
  modtab1_server('tab1')
  modtab2_server('tab2')
}

shinyApp(ui = ui, server = server)

再次编辑 我在一个新问题中问了这个问题,得到了回答:Shiny modules: switch tabs from within modules that have different namespaces

最佳答案

我认为这就是您正在寻找的。做了两个相当小的改变!第一,在 modtab1_server 函数中,我将 ns(id, 'tabs') 更改为 'tabs'。我认为由于 inputId 位于模块内,因此它已经添加了 id,在本例中这意味着它添加了 tab1。使用您现有的代码,我认为 tabsetPanel 的 id 是“tab1-tab1-tabs”,因此通过删除 ns(id) ,它应该将 inputId 调用为“tab1-tabs”。第二个更改是使 tabsetPanel id 为“tab1-tabs”,以封装模块将“tab1”添加到 updateTabsetPanel 的 inputId 的方式。

modtab1_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 1',
           value = NS(id, 'tab.1'),
           
           h4('This is the first tab'),
           actionButton(NS(id, 'nexttab'), 'Next Tab')
           
  )
}

modtab1_server <- function(id) {
  moduleServer(id,
               function(input, output, session) {
                 observeEvent(input$nexttab, {
                   updateTabsetPanel(session = session, inputId = 'tabs', selected = NS(id, 'tab.2'))
                   print('button clicked')
                 })
               })
}

modtab2_ui <- function(id) {
  ns <- NS(id)
  tabPanel(title = 'Tab 2',
           value = NS(id, 'tab.2'),
           
           h4('This is the second tab'),
  )
}


ui <- fluidPage(
  tabsetPanel(
    id = 'tab1-tabs',
    modtab1_ui('tab1'),
    modtab2_ui('tab1')
  )
)

server <- function(input, output, session) {
  modtab1_server('tab1')
}

shinyApp(ui = ui, server = server)

关于R Shiny : Change tabs from within a module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69713537/

相关文章:

r - 如何将 R 数据框上传到谷歌驱动器?

r - Shiny 中的nearPoints() 和brushedPoints() 函数是否适用于条形图?

javascript - R Shiny 的selectizeInput : set minimum for amount of inputs

r - 是否可以在砖石网格中排列Shiny应用程序卡?

javascript - Javascript 中的命名空间问题

php - 解析命名空间标签内的 XML 属性 (PHP)

c# - 命名空间和子命名空间

r - R 中的抓取列表

r - 带条件的 Dplyr 过滤器

r - 通过 selectInput 或 actionButton 更改选择