daattali / shinyalert

🗯️ Easily create pretty popup messages (modals) in Shiny
https://daattali.com/shiny/shinyalert-demo/
Other
241 stars 26 forks source link

Suppressing shinyalert messages in textOutput #56

Closed jono3030 closed 3 years ago

jono3030 commented 3 years ago

Dear Dean,

First of all thanks for the awesome package!

I've got a question about using shinyalert with an actionButton in an observeEvent -> eventReactive environment with validation. The validation checks and shinyalert are part of the same eventReactive and as such share the same renderText function which is linked to a textOutput box that displays the error messages returned by the validation checks. Clicking on the action button triggers the validation checks. If a check doesn't pass, an error message gets displayed in the textOutput. If everything passes, however, no error message from the validation checks gets displayed and shinyalert gets triggered. The idea is that the function that gets triggered by the actionButton only executes when all validation checks have passed.

The problem is that triggering shinyalert also displays one of the following messages in the textOutput that it shares with the validation checks:

__shinyalert-c341eaef213a46c68c007ead25dfba7b
__shinyalert-eec9a1d3909c446ca65b60dc35809d68
__shinyalert-b162039052b14019981d1c86f5d21e51
...
__shinyalert-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

These messages vary depending on the input entered by the user.

My question is if there is a way to suppress this output so that is doesn't show up when all validation checks have passed and the modal pops up?

I assume that the output is expected (auto generated object id's perhaps?) but I'm too much of a Shiny novice to understand where it is coming from or how to capture it. I tried to not include shinyalert in the eventReactive but I couldn't find a way to keep the validity checks working. Anyway, it would be great if there was a way to suppress or control this output.

Also, as I only started using Shiny a month ago or so I apologize in advance if I missed something obvious here and this is not actually a legitimate question.

In either case, here is a reproducible example:

library(shiny)
suppressMessages(library(shinyalert))

ui <- fluidPage(

   useShinyalert(),

   titlePanel("shinyalert reproducible example"),

   sidebarLayout(
      sidebarPanel(
      p("Clicking the button without changing the value results in an error messages and suppresses the modal popup. Change the value to get the popup and see the object returned by shinyalert.")
      ),

      mainPanel(
        numericInput("test",
                   h4("Test input"),
                   value=2700,
                   min=0),
      verbatimTextOutput("test_box"),

      actionButton("go","Execute")

      )
   )
)

server <- function(input, output) {
   v <- eventReactive(input$go, {

      validate(
        need(input$test != 2700, "This value can't be 2700")
      )

     shinyalert(
        title = "The value you entered is:",
        text = input$test,
        type = "success"
      )
   })

   output$test_box <- renderText({
     v()
   })
}

shinyApp(ui = ui, server = server)

Thanks a lot!

daattali commented 3 years ago

What you're seeing is the return value of calling shinyalert(). A call to shinyalert() returns a unique ID of that modal box. It's being printed out simply because it's the last statement inside the eventReactive(). For example, if you add 5 inside the eventReactive after the call to shinyalert, then you would see the text 5 printed out. I don't understand exactly what you're trying to achieve, but this has nothing to do with shinyalert, I think you may need to rethink the entire logic of how you're doing validation checks and printing messages. I suggest asking on an R/shiny online forum for help, since this is a general shiny question, not about shinyalert.

jono3030 commented 3 years ago

Thanks a lot @daattali for taking the time to respond and for your clear explanation! The returned value makes sense now and your answer helped me a lot with understanding this better. Apologies for posting here instead of going to the R/Shiny forums instead.

Thanks again!