I am developing a shiny application in which I use tabsetPanel
, which is generated when user enters some particular input. Thus, I want to use renderUI
function to make a tabsetPanel appear / disappear.
My struggle now is that number of tabPanel
elements (arguments of tabsetPanel
) also depends on user input, in the way: sometimes I need 1 one, other times I want more tabPanels
.
How to make it? I tried including conditionPanel
or including simple if()...
condition in the tabsetPanel
argument, but it (rather not surprisingly...) did not work.
Here you go. The code is fairly self explanatory.
library(shiny)
runApp(list(
ui = pageWithSidebar(
headerPanel('Dynamic Tabs'),
sidebarPanel(
numericInput("nTabs", 'No. of Tabs', 5)
),
mainPanel(
uiOutput('mytabs')
)
),
server = function(input, output, session){
output$mytabs = renderUI({
nTabs = input$nTabs
myTabs = lapply(paste('Tab', 1: nTabs), tabPanel)
do.call(tabsetPanel, myTabs)
})
}
))
There is a way to dynamically add tabPanels without renderUI, which might not be as obvious as the version with renderUI. I wrote a function addTabToTabset which will append any (list of) tabPanel(s) to a tabset/navbar.
This approach has a set of advantages over using renderUI:
The solution and code sample can be found in the answer here. If requested, I could also post the code here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With