4

I'm working on a Shiny app and I would like to know if it's possible or if anyone else has been able to trigger an observeEvent() by switching between tabPanel().

I have had experience in enabling and disabling the tabPanel() once certain actions are executed thanks to @SriPaladugu code and @DeanAttali shinyjs package but I don't know the extent of these two methods/package when it comes to answering my own question.

I need to trigger an observeEvent() in order to execute removeNotification() and remove any warnings windows when switching between tabs.

The way I pictured of doing this was something like:

    observeEvent(input$tabSwitch, {
        removeNotification(previous.warning.message)
    })

However, there is no way to make switching tabs an eventExpr thus making the code above exectuable

If anyone has done this or has knowledge on how to do this, I would greatly appreciate it.

theneil
  • 488
  • 1
  • 4
  • 14

1 Answers1

6

As already mentioned in the comments you'll have to provide an id to the tabsetPanel.

Here is a working example:

library(shiny)

ui <- fluidPage(
  mainPanel(
    tabsetPanel(id = "tabSwitch",
      tabPanel("Tab 1", br(), "Tab 1 content"),
      tabPanel("Tab 2", br(), "Tab 2 content"),
      tabPanel("Tab 3", br(), "Tab 3 content")
    ), br(),
    actionButton("warningBtn", "Generate Warning")
  )
)

server <- function(input, output, session) {

  observeEvent(input$warningBtn, {
    showNotification(ui = paste(Sys.time(), " - Warning!"), duration = NULL, closeButton = FALSE, id = "previousWarningMessage", type = "warning")
  })

  observeEvent(input$tabSwitch, {
    print(paste("You clicked tab:", input$tabSwitch))
    removeNotification("previousWarningMessage")
  })
}

shinyApp(ui, server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78