【问题标题】:display different number of tabPanels in a navbarPage in Shiny, without renderUI or uiOutput在 Shiny 的 navbarPage 中显示不同数量的 tabPanel,没有 renderUI 或 uiOutput
【发布时间】:2015-02-09 02:37:03
【问题描述】:

我正在尝试在 Shiny 的 navbarPage 中显示 1 到 5 tabPanels

我的代码生成了 5 个图,但我希望用户能够选择他们想要访问的图 - 自然会在每个 tabPanel 中显示一个图。

我有一个外部配置文件(config.txt),通过source('config.txt'),我可以访问number_of_pages 变量。

例如number_of_tabPages <- 3

如何在UI.R 中进行设置?

无法在 UI 文件中对 tabPanel 的数量进行硬编码,因为它取决于用户指定的值,而不是使用控件。

我四处搜索,发现大多数此类事情的方法 涉及使用uiOutputrenderUI 函数,例如这个similar 问题,但我不希望UI 中的任何特殊控件进行任何选择。

当我们根据可能发生变化的值构建 UI 时,这就是事情变得棘手的地方。我的大脑正试图将自己包裹在做这类事情的最佳方法上——我觉得这与 Shiny 想要使用 UI 服务器环境与自己交流的方式并不完全一致。

非常感谢任何建议。

如果不是动态的,我的 UI.R 很容易创建:

fluidRow(
column(12,
       "", 
       navbarPage("",tabPanel("First Tab", 
                              plotOutput("plot1")),

                  tabPanel("Second Tab",
                           plotOutput("plot2")),

                  tabPanel("Third Tab",
                           plotOutput("plot3")),

                  tabPanel("Fourth Tab",
                           plotOutput("plot4")),

                  tabPanel("Fifth Tab",
                           plotOutput("plot5"))
                  )
       )
)
)

谢谢!

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    如果您不需要用户以交互方式更改tabPanel 的数量,而只是在应用启动时加载不同数量的do.call,则可以使用navBarPage 中的do.call 函数:

    library(dplyr)
    library(shiny)
    library(ggvis)
    
    #number of tabs needed
    number_of_tabPages <- 10
    
    #make a list of all the arguments you want to pass to the navbarPage function
    tabs<-list()
    #first element will be the title, empty in your example
    tabs[[1]]=""
    
    #add all the tabPanels to the list
    for (i in 2:(number_of_tabPages+1)){
      tabs[[i]]=tabPanel(paste0("Tab",i-1),plotOutput(paste0("plot",i-1)))
    
    }
    
     #do.call will call the navbarPage function with the arguments in the tabs list
    shinyUI(fluidRow(
      column(12,
             "", 
             do.call(navbarPage,tabs)
             )
      )
    )
    

    【讨论】:

      猜你喜欢
      • 2019-02-11
      • 2020-09-29
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 2013-10-28
      • 2021-08-06
      相关资源
      最近更新 更多