Wait for the output of a "confirmation modal" to compute a reactive value in an RShiny application

Viewed 69

I would like to build an RShiny application of this type:

  • the user enters a numerical input and then launches the calculations with a button
  • if the numerical input is below a threshold the calculations are launched automatically
  • if the numerical input is above the threshold, a modal box appears to tell the user that it will take time. He can then cancel (no calculation is launched) or continue anyway.

Here is a schematic example of an application.

library(shiny)
library(shinyWidgets)


ui <- fluidPage(
  tags$h1("Ask the user for confirmation"),
  numericInput(
    inputId = "number", label = "Enter your number", value = 5,
    min = 1, max = 10, step = 1
  ),
  actionButton(
    inputId = "start",
    label = "Start"
  ),
  verbatimTextOutput(outputId = "res")
)

server <- function(input, output, session) {
  
  random <- eventReactive(input$start, {
    
    if (input$number > 6){
      ask_confirmation(
        inputId = "myconfirmation",
        title = "Want to confirm ?",
        text = "It will take a long time..."
      )
      req(input$myconfirmation)
    } 
    rnorm(input$number)
    
  })
  
  output$res <- renderPrint(random())
}

shinyApp(ui = ui, server = server)
  

However, in this example, the computation of my reactive variable random() is not at all conditional on the user's response in the modal box but rather on the past states of the variable. I specify that I used the ask_confirmation function of the shinyWidgets package here for its explicit character but I also tried functions of the basic shiny package, or of shinyalert. I feel I should probably decouple by creating observeor observeEvent blocks outside my reactive variables but I haven't managed to do that yet. Could someone point me in the right direction?

Thanks a lot!

0 Answers
Related