Home > Software engineering >  modalDialog inside a loop in shinyapp
modalDialog inside a loop in shinyapp

Time:05-25

Could someone help me for the love of god? How can I use a modalDialog inside a loop? I've looked at some forums but none of them were satisfactory or didn't fit my problem. Below is a minimal reproducible code that simulates my problem. The solution presented in RShiny: How to have sequential Modals in for loop didn't work as the actionbutton I put in the text argument of the shinyalert function was not recognized in the observeEvent.

library(shiny)

dialog_filtro <- function(ID,LabelID,messagee){ 
  modalDialog(
          title = "Menssagem importante",
          messagee,
          footer = tagList(
                           actionButton(ID[1],LabelID[1]),
                           actionButton(ID[2],LabelID[2])
                           )
           )
}

ui <- fluidPage(
           uiOutput('res')
)

server <- function(input, output, session) {
  
      RESFIL <- reactiveValues(dest = NULL)
      lista <- list(a=2,a=3)
      grupdest <- rep(list(NA),length(lista))
      RESFIL$dest <- grupdest
      
      for(i in 1:length(lista)){
          
          if(lista[[i]] > 0){
            
             showModal(dialog_filtro(ID = c(paste0('yes',i),paste0('no',i)),
                     LabelID = c('Yes','No'),
                     messagee = paste0('This is the loop ',i)
                     ))
             
             observeEvent(input[[paste0('yes',i)]], { 
                 RESFIL$dest[[i]] <- i 10
                 removeModal()              
             })
             
             observeEvent(input[[paste0('no',i)]], { 
                 RESFIL$dest[[i]] <- i 100
                 removeModal()             
             })
             
             }else{
                 RESFIL$dest[[i]]  <- i 1000
                 removeModal() 
             }
          }
          
        output$res <- renderPrint({ RESFIL$dest })  
  }

shinyApp(ui = ui, server = server)

Thanks in advance for any attempt to help.

CodePudding user response:

Here is a way with shinyalert. That's strange, if you put a button in a Shiny alert, then clicking the button automatically closes the alert. That's why I use actionLink instead of actionButton. The app does not react as usual when clicking one button/link, so I use the onclick attribute which runs Shiny.setInputValue. As you can see I'm using local in the loop, otherwise that does not work as expected. But I think (I didn't test) that instead of using for local, you can use an ordinary lapply.

library(shiny)
library(shinyalert)

dialog_filtro <- function(i, input, session, RESFIL, ID, LabelID, messagee) {
  al <- shinyalert(
    title = "Menssagem importante",
    text = tagList(
      tags$p(messagee),
      actionLink(
        ID[1], LabelID[1], class = "btn btn-primary",
        onclick = sprintf(
          'Shiny.setInputValue("%s", true, {priority: "event"});', 
          ID[1]
        )
      ),
      actionLink(
        ID[2], LabelID[2], class = "btn btn-primary",
        onclick = sprintf(
          'Shiny.setInputValue("%s", true, {priority: "event"});', 
          ID[2]
        )
      ),
    ),
    html = TRUE,
    session = session
  )
  observeEvent(input[[ID[1]]], {
    RESFIL$dest[[i]] <- i   10
    closeAlert(id = al)
  }, domain = session)
  observeEvent(input[[ID[2]]], {
    RESFIL$dest[[i]] <- i   100
    closeAlert(id = al)
  }, domain = session)
}

ui <- fluidPage(
  verbatimTextOutput("res")
)

server <- function(input, output, session) {
  
  RESFIL <- reactiveValues(dest = NULL)
  lista <- list(a = 2, a = 3)
  grupdest <- rep(list(0), length(lista))
  RESFIL$dest <- grupdest
  
  for (i0 in 1:length(lista)) {
    local({
      
      i <- i0
      
      if (lista[[i]] > 0) {
        
        dialog_filtro(
          i,
          input,
          session,
          RESFIL, 
          ID = c(paste0("yes", i), paste0("no", i)),
          LabelID = c("Yes", "No"),
          messagee = paste0("This is the loop ", i)
        )
        
      } else {
        RESFIL$dest[[i]] <- i   1000
        # removeModal() not clear what you want to do here: remove which modal?
      }
      
    })
  }
  
  output$res <- renderPrint({
    RESFIL$dest
  })
}

shinyApp(ui = ui, server = server)
  • Related