I'm writing a shiny app in which I want the user to be able to add and remove tabs. I know how to add tabs if the user clicks on a tab specifically made for that (thanks to the answer here) but I can't figure out how to remove the lastly created tab.
Here's a reproducible example:
library(shiny)
library(shinyWidgets)
ui <- navbarPage(position = "static-top",
title = "foo",
id = "tabs",
tabPanel(title = "Name 1",
fluidRow()),
tabPanel(title = "More",
icon = icon("plus"),
fluidRow()),
tabPanel(title = "Less",
icon = icon("minus"),
fluidRow())
)
server <- function(input, output) {
count <- reactiveVal(1)
observeEvent(input$tabs, {
if (input$tabs == "More"){
count(count()+1)
id = paste0("Name ", count())
insertTab(inputId = "tabs",
tabPanel(title = id,
fluidRow(column(
width = 12))
), target = "More", position = "before",
select = TRUE)}
if (input$tabs == "Less"){
count(count()+1)
id = paste0("Name ", count())
removeTab(inputId = "tabs",
target = id
)}
})
}
shinyApp(ui = ui, server = server)
Here, you can see that clicking on the tab More adds a tab named Name i with i the number of clicks made on the tab More. However, clicking on the tab Less does nothing.
What I would like is the following:
if the user clicks at least once on
More, then clicking onLessremoves the last created tab (therefore placed beforeMore)if the user does not click on
Morethen clicking onLessdoes nothingimagine that I click twice on
Morethen there will be two additional tabs namedName 2andName 3. Clicking onLesswill removeName 3but if I click again onMore, the additional tab will be again calledName 3(therefore clicking onLessshould not prevent the re-use of the name of the tab removed).
Does anybody know how to do it?