我想显示从1到5 tabPanels
的navbarPage
闪亮.
我的代码生成了5个图,但我希望用户能够选择他们想要访问的数量 - tabPanel
自然地在每个图中显示一个图.
我有一个外部配置文件(config.txt
),通过source('config.txt')
,我可以访问一个number_of_pages
变量.
例如, number_of_tabPages <- 3
我该如何设置UI.R
?
在UI文件中根本无法对tabPanel进行硬编码,因为它取决于用户指定的值,而不是使用控件.
我一直在搜索并发现大多数这类事情的方法都涉及使用uiOutput
和renderUI
函数,比如这个类似的问题,但我不希望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"))
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
谢谢!
如果您不需要用户以tabPanel
交互方式更改数量,但只需在应用程序启动时加载不同数量的数据,您可以使用以下do.call
功能navBarPage
:
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)
)
)
)
Run Code Online (Sandbox Code Playgroud)