dreamRs / shinyWidgets

shinyWidgets : Extend widgets available in shiny
https://dreamrs.github.io/shinyWidgets/
GNU General Public License v3.0
834 stars 153 forks source link

allow additional text in progressSweetAlert and updateProgressBar #671

Closed agilly closed 8 months ago

agilly commented 8 months ago

Hi!

This is more of a feature request than an issue. Currently, we are able to create sweetAlerts of various types with a title and text:

sendSweetAlert(
            session = session,
            title = "Error",
            text = "Please select at least one item",
            type="error"
        )

We are also able to use a progress bar modal in a sweet alert:

##UI
 useSweetAlert()
####

##server
            progressSweetAlert(
                session=session,
                id="myProgress",
                title="Doing stuff",
                display_pct=T,
                value=0, status="info", striped=T
            )
for(i in 1:10){
                perform_long_computation()
                updateProgressBar(session=session, id="myProgress", title="Calculating...",value=i*10)
}

In the documentation, it says that progressSweetAlert's remaining arguments are passed to sendSweetAlert: ... Arguments passed to sendSweetAlert()

However that doesn't extend to the text argument, because if I use it, I get:

Warning: Error in sendSweetAlert: formal argument "text" matched by multiple actual arguments

My guess is that text is overloaded to contain the progress bar. Is there a way, however, to make progressSweetAlert and updateProgressBar accept some additional text? It would help building more dynamic and informative progress bars.

Even if this suggestion is rejected, any workaround is also appreciated!

pvictor commented 8 months ago

Hello, Yes you're right, text is used to put the progress bar, there's no easy way to put some text at the moment, beside of title. But progressSweetAlert() is just a wrapper around sendSweetAlert() and progressBar(), so you can easily make a version that suit your needs, for example something like :

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  actionButton("show", "Show progress")
)

server <- function(input, output, session) {
  observeEvent(input$show, {
    sendSweetAlert(
      session = session,
      title = NULL,
      btn_labels = NA,
      text = tags$div(
        progressBar(
          id = "myprogress",
          title = "Work in progress",
          display_pct = TRUE, 
          value = 0
        ),
        tags$div(
          id = "mytext", 
          tags$p("Here you can put some text")
        )
      ),
      closeOnClickOutside = FALSE,
      backdrop = TRUE
    )
    for (i in seq_len(50)) {
      Sys.sleep(0.1)
      updateProgressBar(
        session = session,
        id = "myprogress",
        title = "Work in progress",
        value = i*2
      )
      if (i == 25) {
        removeUI("#mytext p", immediate = TRUE)
        insertUI(
          selector = "#mytext", 
          ui = tags$p("This text has been updated"),
          immediate = TRUE
        )
      }
    }
    closeSweetAlert(session = session)
  })
}

shinyApp(ui, server)

There's also some alternative for a progress modal here : https://dreamrs.github.io/shinybusy/reference/modal-progress.html if you're interested.

Victor

agilly commented 8 months ago

Thank you very much @pvictor , this is super useful. I tried the shinybusy solution and it works great! Will update my current progress bars with the wrapper you suggested soon.

agilly commented 8 months ago

Also tested your solution for the progress bar, it works well. The style of "#mytext" is a bit heavy but I assume p() can be replaced or styled to a lighter/smaller font.