【问题标题】:changing the sidebarPanel with navbarPage in shiny用闪亮的 navbarPage 更改 sidebarPanel
【发布时间】:2015-02-05 21:06:57
【问题描述】:

我正在使用侧边栏面板,使用 navbarPage。

我想只“声明”一个小部件(本例中为单选按钮),然后将每个选项卡链接到“主”侧边栏面板。如您所见,tab2 对相关单选按钮没有反应。 我的项目的结构更复杂,有必要为某些选项卡设置相同的侧边栏面板,而为其他一些选项卡设置特定的侧边栏面板。 这是我正在使用的代码:

library(shiny)

server=function(input, output) {  
  output$plot1 = renderPlot({plot(runif(input$rb))})
  output$plot2 = renderPlot({plot(runif(input$rb))})
}



ui = shinyUI(navbarPage("Test multi page",                        
  tabPanel("tab1",                      
          sidebarLayout(
            sidebarPanel(
              radioButtons("rb","Nr of obs:",choices = c("50 obs"=50,"300 obs"=300))
            ),
            mainPanel(plotOutput("plot1"))
          )
          ),

  tabPanel("tab2",                      
    sidebarLayout(
      sidebarPanel(
        radioButtons("rb","Nr of obs:",choices = c("50 obs"=50,"300 obs"=300))
       ),
      mainPanel(plotOutput("plot2"))
    )
  )

))

shinyApp(ui = ui, server = server)

runApp("app")

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您不能以您想要的方式重复使用 ui 元素。您可以获得的最接近的方法是使用条件面板。在下面的示例中,fixed 将是您希望在多个页面上拥有的输入。条件面板将允许您基于活动选项卡添加 ui 组件,其中tabid 是您的 tabsetPanel 的 id-label。您不必在这里使用 renderUI 和 uiOutput,但如果您有很多选项卡和 ui 元素,它可能会让您更轻松。要查看示例,请查看 this app 并单击“数据”菜单的选项卡。

    sidebarPanel(
      uiOutput("fixed"),
      conditionalPanel("input.tabid == 'tab1'", uiOutput("ui_tab1")),
      ....
    )
    mainPanel(
      tabsetPanel(id = "tabid",
      ...
    )
    

    【讨论】:

      【解决方案2】:

      问题是您有两个输入小部件,其中inputId 设置为rb。如果把tab2中的代码改成:

      radioButtons("rb2","Nr of obs:",choices = c("50 obs"=50,"300 obs"=300))
      

      然后将其连接到server 中的output$plot2

      output$plot2 = renderPlot({plot(runif(input$rb2))})
      

      该应用程序将运行。

      我认为发生这种情况的原因是因为即使小部件不可见,闪亮也会记住小部件的值。所以你很容易在tab1 中得到一个rb 的值,在tab2 中得到一个不同的rb 值。在这种情况下,不清楚应该绘制什么,因此第二个rb 将被忽略。

      【讨论】:

      • 嗨,我的例子好像不是很清楚,抱歉。我的项目非常“复杂”,即有大量选项卡。我只想声明一次小部件,然后将其链接到我的选项卡(无需在每个选项卡中重复 radioButtons 命令)。
      猜你喜欢
      • 1970-01-01
      • 2018-10-22
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      相关资源
      最近更新 更多