R Shiny:嵌套的tabPanel互相禁用

时间:2016-06-27 03:13:14

标签: r shiny

在附加的MWE Shiny示例中,我在导航栏的tabPanel中有一个嵌套的tabsetPanel。如果您在tabSet中仅使用一个tabPanel运行MWE,您将看到Shiny的行为完全符合预期。但是,如果使用两个tabPanel运行MWE,则结果不会打印到每个选项卡的主面板。

为什么会出现这种情况?我该如何解决这个难题?

library(shiny)

ui <- shinyUI(navbarPage("tabalicious", 
                     tabPanel("Nav1", value = "nav1", 
                              mainPanel(h2("Hello"),
                                        br(),
                                        p("This is my app.")
                              )
                     )
                     ,
                     tabPanel("Nav2", value = "nav2", 
                              tabsetPanel(
                                tabPanel("tabsettab1",
                                         sidebarLayout(
                                           sidebarPanel(
                                             helpText("Choose your settings"),
                                             selectInput("zone_type",
                                                         label = "Choose a zone type to display",
                                                         choices = list("Industrial", "Residential"),
                                                         selected = "Industrial")
                                           ),
                                           mainPanel(h2("A tab for a tabSet"),
                                                     textOutput('zone_type')
                                           )
                                         )
                                )
                                # Uncomment this to see the issue
                                # ,
                                # tabPanel("tabsettab2",
                                #          sidebarLayout(
                                #            sidebarPanel(
                                #              helpText("Choose your settings"),
                                #              selectInput("zone_type",
                                #                          label = "Choose a zone type to display",
                                #                          choices = list("Industrial", "Residential"),
                                #                          selected = "Industrial")
                                #            ),
                                #            mainPanel(h2("A tab for a tabSet"),
                                #                      textOutput('zone_type')
                                #            )
                                #          )
                                # )
                                  )
                         )
)
)

server <- shinyServer(function(input, output) {

  output$zone_type <- renderText({ 
    paste("You have selected", input$zone_type)
  })
})

# Run the application 
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

它不与标签有关,而是多次调用以输出相同render*函数的结果。例如,简化的页面(没有选项卡)可以正常工作,但是如果取消注释重复的调用,将无法显示zone_type

library(shiny)

server <- shinyServer(function(input, output) {
  output$zone_type <- renderText({paste("You have selected", input$zone_type)})
})

ui <- shinyUI(fluidPage(
  selectInput("zone_type",
              label = "Choose a zone type to display",
              choices = list("Industrial", "Residential")),
  # textOutput('zone_type'),
  textOutput('zone_type')
))

runApp(shinyApp(server = server, ui = ui))

虽然shinyUI函数只能调用shinyServer的每个输出,但在shinyServer内,您可以根据需要多次调用输入,因此可以轻松复制输出:< / p>

library(shiny)

server <- shinyServer(function(input, output) {
  output$zone_type <- renderText({paste("You have selected", input$zone_type)})
  output$zone_type2 <- renderText({paste("You have selected", input$zone_type)})
})

ui <- shinyUI(fluidPage(
  selectInput("zone_type",
              label = "Choose a zone type to display",
              choices = list("Industrial", "Residential")),
  textOutput('zone_type'),
  textOutput('zone_type2')
))

runApp(shinyApp(server = server, ui = ui))

如果您不想复制服务器的工作,则无法将一个输出传递给另一个,但您可以将render*结果保存到一个可以传递给两个输出的局部变量:

server <- shinyServer(function(input, output) {
  zone <- renderText({paste("You have selected", input$zone_type)})
  output$zone_type <- zone
  output$zone_type2 <- zone
})