Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

R Shiny: remove newly created tabs

$
0
0

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 on Less removes the last created tab (therefore placed before More)

  • if the user does not click on More then clicking on Less does nothing

  • imagine that I click twice on More then there will be two additional tabs named Name 2 and Name 3. Clicking on Less will remove Name 3 but if I click again on More, the additional tab will be again called Name 3 (therefore clicking on Less should not prevent the re-use of the name of the tab removed).

Does anybody know how to do it?


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>