【问题标题】:updateTabsetPanel with Shiny module带有 Shiny 模块的 updateTabsetPanel
【发布时间】:2019-02-18 23:57:40
【问题描述】:

在 Shiny 模块中调用 updateTabsetPanel 时遇到问题,不用也可以正常工作。

library(shiny)

mod_ui <- function(id){
  ns <- NS(id)
  tagList(
    actionButton(ns("back"), "back")
  )
}

mod <- function(input, output, session){
  observeEvent(input$back, {
    print("Button click, go back to home tab")
    updateTabsetPanel(session = session, inputId = "tabs", selected = "home")
  })
}

ui <- navbarPage(
  "example",
  id = "tabs",
  tabPanel(
    "home",
    h4("updateTabsetPanel does not work with modules"),
    h5("But the button below does"),
    actionButton("switch", "switch")
  ),
  tabPanel(
    "secondtab",
    mod_ui("second")
  )
)

server <- function(input, output, session){
  callModule(mod, "second")
  observeEvent(input$switch, {
    updateTabsetPanel(session = session, inputId = "tabs", selected = "secondtab")
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    模块的设计方式使得每个模块都是完全独立的。如果您需要与调用模块的父级通信,则需要显式传递参数。以下是它的完成方式:

    library(shiny)
    
    mod_ui <- function(id){
      ns <- NS(id)
      tagList(
        actionButton(ns("back"), "back")
      )
    }
    
    mod <- function(input, output, session,parent_session){
      observeEvent(input$back, {
        print("Button click, go back to home tab")
        updateTabsetPanel(session = parent_session, inputId = "tabs", selected = "home")
      })
    }
    
    ui <- navbarPage(
      "example",
      id = "tabs",
      tabPanel(
        "home",
        h4("updateTabsetPanel does not work with modules"),
        h5("But the button below does"),
        actionButton("switch", "switch")
      ),
      tabPanel(
        "secondtab",
        mod_ui("second")
      )
    )
    
    server <- function(input, output, session){
      callModule(mod, "second",parent_session = session)
      observeEvent(input$switch, {
        updateTabsetPanel(session = session, inputId = "tabs", selected = "secondtab")
      })
    }
    
    shinyApp(ui, server)
    

    session 被显式传递给模块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2017-07-27
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      相关资源
      最近更新 更多