Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make actionbutton only work every time it is pressed?

Hello and thanks for reading me. I am currently trying to make a simple app in shiny that allows you to filter a dataframe, but I would like the filter to update every time I press the button. It works the first time, but apparently afterwards the observeEvent stays activated and the information is filtered even if you don’t press the button. Is there any way to avoid this?

The code is the following:

library(shiny)
library(dplyr)
library(shinyWidgets)

x <- tibble(
  val1 = 1:5,
  val2 = sample(letters,5)
)

shinyApp(
  ui = fluidPage(
    column(width = 3, pickerInput("filt", "filter",
                                  choices = x$val1,
                                  selected = x$val1,
                                  multiple = TRUE
                                  ),
           actionButton("ready", "filter data")
           ),
    column(width = 6, textOutput("text"))
  ),
  server = function(input, output, session){
    
    
    observeEvent(input$ready,{
      
      output$text <- renderText({
        
        x <- x |> 
          filter(val1 %in% input$filt)
        
        
        print(x$val2)
        
      })
      
      
    })
    
    
    
  }
)

I think the problem is in this part:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

observeEvent(input$ready,{
      
      output$text <- renderText({
        
        x <- x |> 
          filter(val1 %in% input$filt)
        
        
        print(x$val2)
        
      })
      
      
    })

Thanks a lot for the help

>Solution :

Use the bindEvent function in shiny

library(shiny)
library(dplyr)
library(shinyWidgets)

x <- tibble(
  val1 = 1:5,
  val2 = sample(letters,5)
)

shinyApp(
  ui = fluidPage(
    column(width = 3, pickerInput("filt", "filter",
                                  choices = x$val1,
                                  selected = x$val1,
                                  multiple = TRUE
    ),
    actionButton("ready", "filter data")
    ),
    column(width = 6, textOutput("text"))
  ),
  server = function(input, output, session){
    
    output$text <- renderText({
      
      x <- x |> 
        filter(val1 %in% input$filt)
      
      print(x$val1)

    }) |> 
      bindEvent(input$ready)
    
    
    
    
  }
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading