pickerinput as filter disable all not disabling button using shinyJS

442 Views Asked by At

hope you are keeping safe.

I've searched for similar issues but without success.

My code is 5k lines long so posting it is a no go.

I'll try to provide as much info as possible.

In a nutshell, I have some pickerInputs working as a reactive filters for a rhandsontable object.

I have a Save Button so users save their input.

My initial strategy had an Achilles heel which was that saving with filtering on would overwrite all the data in rhandsontable.

So, a quick workaround was to add an observeEvent where the Save Button (by using ShinyJS) is only enabled when the pickerInput has all options selected.

So far, so good but I noticed a strange behaviour: initially if we click "Deselect all" in pickerInput the Save Button is disabled but by clicking "Select All" (which enables the Save Button) and then "Deselect All" then the Save Button is not disabled.

My observeEvent code below.

Does anyone seem this behaviour before?

## having buttons disabled by default to account for null/empty statement
  shinyjs::disable("save_data_PCD_Metrics") 
  
  observeEvent(input$metricsTableEXCELPCDProject, {
    req(input$metricsTableEXCELPCDProject)
    print(length(input$metricsTableEXCELPCDProject))
    print(length(sort(unique(as.character(metricsTableEXCEL$Project)))))
    print(isTRUE(length(input$metricsTableEXCELPCDProject) == length(sort(unique(as.character(metricsTableEXCEL$Project))))))
    print(input$metricsTableEXCELPCDProject)
    
    ## having buttons disabled by default to account for null/empty statement
    shinyjs::disable("save_data_PCD_Metrics")
    
    ## toggle state of the button when condition of all projects selected 
    if(isTRUE(length(input$metricsTableEXCELPCDProject) == length(sort(unique(as.character(metricsTableEXCEL$Project)))))) {
      shinyjs::enable("save_data_PCD_Metrics")
    }
    
  })
  

Best,

R

1

There are 1 best solutions below

1
On BEST ANSWER

To fix your issue, you can enable the save button in a separate observer, and not inside the observeEvent. Try this

library(shiny)
library(shinyjs)
library(shinyWidgets)

df1 <- data.frame(A=c(1:15),Y=c(16:30))

ui <- fluidPage(
  useShinyjs(),
  pickerInput("test",choices=list("A"=list(1,2,3,4,5),"B"=list(6,7,8,9,10), "C" = list(11,12,13,14,15)),
              options = list(`actions-box` = TRUE 
                             
                             ),
              multiple=TRUE),
  actionButton("savee", "Save"),
  textOutput("testOutput") 
)

server <- function(input, output) {
  
  shinyjs::disable("savee") 
  
  observeEvent(input$test, {
    req(input$test)
    print(length(input$test))
    #print(length(sort(unique(as.character(metricsTableEXCEL$Project)))))
    print(isTRUE(length(input$test) == length(sort(unique(as.character(df1$A))))))
    print(input$test)
    
    ## having buttons disabled by default to account for null/empty statement
    # shinyjs::disable("savee")
    
    ##  toggle state of the button when condition of all projects selected  
    ##  enabling within this observeEvent will not work
    # if(isTRUE(length(input$test) == length(sort(unique(as.character(df1$A)))))) {
    #   shinyjs::enable("savee")
    # }
    
  })
  
  saveme <- reactive({
    if(isTRUE(length(input$test) == length(sort(unique(as.character(df1$A)))))) saveme = 1 else saveme = 0
  })
   
  observe({
    if (saveme()) {shinyjs::enable("savee")  ## this works
    }else shinyjs::disable("savee")
    print(saveme())
  })
  
  output$testOutput <- renderText({paste(input$test)})
}

shinyApp(ui = ui, server = server)