带有Shiny模块的updateTabsetPanel

时间:2019-02-18 15:50:33

标签: r shiny

在Shiny模块中调用SELECT r.id FROM recipes r JOIN groups g ON g.recipe_id = r.id JOIN steps s ON s.group_id = g.id JOIN steps_ingredients_memberships sim ON sim.step_id = s.id JOIN ingredients i ON i.id = sim.ingredient_id GROUP BY r.id HAVING BOOL_OR(i.id IN (5, 6)) AND BOOL_OR(i.id IN (10, 11)); 时出现问题,如果没有,效果很好。

updateTabsetPanel

1 个答案:

答案 0 :(得分:3)

模块的设计方式使得每个模块都是绝对独立的。如果需要与调用模块的父级进行通信,则需要显式传递参数。这是完成的方式:

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已显式传递到模块。