R Shiny showModal and removeModal for all users

Viewed 2587

My general problem is that I would like a modal window to show up after all users have entered an enter button then close after all users click actionButton.

I'd really love commands like

removeModal(domain="Everyone") and showModal(domain="Everyone")

My approach is to fill a vector Class$Ready until all values are TRUE and then create the window. Based on the commentors, I can get shiny to display a modal window on all users pages once that condition is satisfied. But I cannot remove it for all users.

server.R

## Global Variables
Class <<- data.frame(ID=1:10, Ready=rep(FALSE,10) )
GlobClass <<- reactiveValues(Ready=Class$Ready, New=Class$Ready) )

## When Enter is Clicked, Update
observeEvent( input$enter, {
    GlobClass$Ready[ Class$ID == user] <- TRUE
})

## When Everyone has clicked enter, showModal
observeEvent (GlobClass$Ready, {
    if( all(GlobClass$Ready) ){
        showModal( modalDialog(
            h2("Effort"), "Try Harder",
            footer=tagList( actionButton("new", "New Round"))
        ))
     }
})

## When New is Clicked, Update and Hide
observeEvent( input$new, {                    
    GlobClass$New[Class$ID==user] <- TRUE
    shinyjs::hide('new')
})

## When Everyone has clicked New, removeModal and reset
observe( {  invalidateLater(efreq)
    if( all(GlobClass$New) ){
        GlobClass$Ready <- rep(FALSE, nrow(Class))
        GlobClass$New  <- rep(FALSE, nrow(Class))
        removeModal()
    }
})

The issue I'm having is that the modalWindow is only removed for one person not all. That's also true if I change observe( { invalidateLater(efreq) to observeEvent( GlobClass$New, {

Edit: Answer

You have to stagger the calls

observeEvent( GlobClass$New, {
#observe( { invalidateLater(efreq)
    if( all(GlobClass$New) ){
        GlobClass$Ready <- rep(FALSE, nrow(Class))
    }
})

observeEvent( GlobClass$Ready , {
    if( all(!GlobClass$Ready) ){
        GlobClass$New <- rep(FALSE, nrow(Class))
        removeModal()
    } 
})
1 Answers

you can maybe do something like this. I print out the number of sessions to see how many are connected:

library(shiny)

ui <- fluidPage(
  mainPanel(actionButton("Submit","Submit"),textOutput("SessionCount"))
)
vals <- reactiveValues(count=0)

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

  isolate(vals$count <- vals$count + 1)

  session$onSessionEnded(function(){
    isolate(vals$count <- vals$count - 1)
  })

  observeEvent(input$Submit,{
    if(vals$count !=0){
      vals$count <- vals$count - 1
    }
  },ignoreInit = T)


  observeEvent(vals$count,{
    if(vals$count ==0){
      showModal( modalDialog( h2("Effort"), "Try Harder"))
    }
  })

  output$SessionCount <- renderText({
    paste0("Number of Current Sessions: ",vals$count)
  })

}

shinyApp(ui, server)

enter image description here

Related