Stop default execution of tab

In my shiny app, I have two tabs. In the first tab when button is pressed it changes dropdown in the second tab (from 'setosa' to 'versicolor') and accordingly filter on the data is applied and shown on table. I want filter using default selected value of dropdown ('setosa') should NOT execute when button of first tab is pressed. In powerful machines processing happens quickly so you may not catch it but user feels "two processing" on slower machines or mobile devices. Is there any way to stop execution of the default selected value?

library(shiny)
library(DT)
library(dplyr)

iris2 <- data.frame()
for(i in 1:10000) {
  iris2 <- bind_rows(iris2,iris)
}

theme <- bslib::bs_theme(version = 4)

ui <- fluidPage(
  title = "Hello Shiny!",
  theme = theme,
  tabsetPanel(
    tabPanel("Plot", 
             uiOutput("tab1")),
    tabPanel("Table", 
             
             selectInput("lists", "Select Category",
                         choices = unique(iris2$Species),
                         selected = "setosa"
                         ),
             
             DTOutput('tbl'))
  )
)

server <-  function(input, output, session) {
  
  
  observeEvent(input$foo, {
    updateSelectInput(session, "lists", selected = input$foo)
  })
  
  output$tbl = renderDT(
    iris2 %>% filter(Species %in% input$lists), 
    options = list(lengthChange = FALSE)
  ) 
  
  output$tab1 <- renderUI({
    
  tagList(
    actionButton("mybtn", "Hit me"),
  
    tags$script("
    $('#mybtn').on('click', function() {
    Shiny.setInputValue('foo', 'versicolor', {priority: 'event'});
    var tabs = $('ul.nav.nav-tabs li a');
    $(tabs[1]).click();
  });")    
  )
    
  })    
  
}

shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.