1

I am trying to implement something similar to this within the app and not at the browser level as described here.

After capturing the value of the new tab (tabPanel value) selected, could not display the confirmation message before switching to the newly selected tab to display its content.

library(shiny)
library(ggplot2)
library(shinyalert)

ui <- fluidPage(useShinyalert(),
  tabsetPanel(id = "tabselected",
    tabPanel("Tab1"),
    tabPanel("Tab2",plotOutput("plot"))
  )
)

server <- function(input, output) {

  observeEvent(input$tabselected, {
    if(input$tabselected == "Tab2") 
      { 
       shinyalert(title = "Save your work before changing tab", type = "warning", showConfirmButton = TRUE)
       output$plot <- renderPlot({ ggplot(mtcars)+geom_abline() })
      }
  })

}

shinyApp(ui = ui, server = server)
RanonKahn
  • 853
  • 10
  • 34

1 Answers1

3

You can simply redirect to Tab1 via updateTabsetPanel as long as your desired condition is met.

Here is an example requiring the user to type something in the textInput before it's allowed to switch the tab.

library(shiny)
library(ggplot2)
library(shinyalert)

ui <- fluidPage(useShinyalert(),
                tabsetPanel(
                  id = "tabselected",
                  tabPanel("Tab1", p(), textInput("requiredText", "Required Text")),
                  tabPanel("Tab2", p(), plotOutput("plot"))
                ))

server <- function(input, output, session) {
  observeEvent(input$tabselected, {
    if (input$tabselected == "Tab2" && !isTruthy(input$requiredText)) {
      updateTabsetPanel(session, inputId = "tabselected", selected = "Tab1")
      shinyalert(title = "Save your work before changing tab",
                 type = "warning",
                 showConfirmButton = TRUE)
      output$plot <- renderPlot({
        ggplot(mtcars) + geom_abline() + ggtitle(req(input$requiredText))
      })
    }
  })
}

shinyApp(ui = ui, server = server)

Result

By the way an alternative approach wpuld be using showTab and hideTab to display the tabs only if all conditions are fulfilled.

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • Great suggestions. However, I am looking for a solution to caution the user when switching between tabs accidentally when they are in middle of something in a tab section. – RanonKahn Jan 29 '20 at 16:24
  • Can you please elaborate on the desired outcome then? – ismirsehregal Jan 30 '20 at 08:57
  • As specified in the question, when the user accidentally (or purposefully) clicks another tab within the app, they should be warned with a message to save the work similar to as it happens here : [https://stackoverflow.com/questions/55048802/r-shiny-ask-confirmation-before-closing-app-tab]. – RanonKahn Jan 30 '20 at 23:09
  • So you don't want that shinyalert but a browser message instead? – ismirsehregal Jan 31 '20 at 10:02
  • I am able to create the browser warning message when a user clicks on the back button in the browser. This is switching of tabs within the app to view various tab contents. Not the change at the browser level. – RanonKahn Jan 31 '20 at 17:58
  • I still don't understand what you are trying to achieve, but good luck. – ismirsehregal Jan 31 '20 at 21:15
  • But this 'tab-toggel' is not part of your example code above. You might consider updating your question - we can't guess your intentions. – ismirsehregal Feb 02 '20 at 10:03