0

I am testing the feasibility of generating random sentences from a list and rendering those in a progressSweetAlert. So far, I have been unable to get further than having the first randomly selected sentence post as the 'value' object.

What I am trying to achieve is, as the progress bar ... progresses, the randomly selected sentences render for a couple of seconds and then proceeds to the next string...such as...

"Eating bugs..." "Watching paint dry..." "Thinking big thoughts.."

Using the LaF package, I have succesfully created a list of sentencs and called it:

x<-c('Locating evil driods.',
     'Planting happy thoughts.',
     'Checking the water for bugs.',
     'Reading "A Tale of Two Cities" ',
     'Questioning the matrix.',
     'Generating apple clones.',
     'Discovering new things.')

writeLines(x, con="tmp.csv")

As per BDS masterly guidance, here is a working example :) :

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(LaF)


ui <- fluidPage(
  tags$h1("Progress bar in Sweet Alert"),
  useSweetAlert(), # /!\ needed with 'progressSweetAlert'
  actionButton(
    inputId = "go",
    label = "Launch long calculation !"
  )
)

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

  observeEvent(input$go, {
    x<-sample_lines("tmp.csv", 5)
    y <- paste(x, 1:length(x), sep = "")

    progressSweetAlert(
      session = session, id = "myprogress",
      title = y,
      display_pct = TRUE, value = 0
    )
    for (i in seq_len(50)) {
      Sys.sleep(0.1)
      updateProgressBar(
        session = session,
        id = "myprogress",
        value = i*2
      )
    }
    closeSweetAlert(session = session)
    sendSweetAlert(
      session = session,
      title =" Calculation completed !",
      type = "success"
    )
  })

}

shinyApp(ui = ui, server = server)

I am hoping to get something not dissimilar from what you can see in these examples (https://blog.teamtreehouse.com/make-loading-screen-unity).

However, this is what I got:

Progress1 Progress2

OctoCatKnows
  • 399
  • 3
  • 17

1 Answers1

1

Maybe you coul just use the titleparameter?

You set title = sentences[sample(length(sentences), 1)],

updateProgressBar could read:

updateProgressBar(
  session = session,
  title = sentences[sample(length(sentences), 1)],
  id = "myprogress",
  value = i*10
)

Full example would read:

library("shiny")
library("shinyWidgets")


ui <- fluidPage(
tags$h1("Progress bar in Sweet Alert"),
useSweetAlert(), # /!\ needed with 'progressSweetAlert'
actionButton(
  inputId = "go",
  label = "Launch long calculation !"
)
)

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

observeEvent(input$go, {
  progressSweetAlert(
    session = session, id = "myprogress",
    title = "Work in progress",
    display_pct = TRUE, value = 0
  )


  sentences <- c('Locating evil driods.',
                 'Planting happy thoughts.',
                 'Checking the water for bugs.',
                 'Reading "A Tale of Two Cities" ',
                 'Questioning the matrix.',
                 'Generating apple clones.',
                 'Discovering new things.')

  for (i in seq_len(10)) {
    Sys.sleep(1)
    updateProgressBar(
      session = session,
      title = sentences[sample(length(sentences), 1)],
      id = "myprogress",
      value = i*10
    )
  }
  closeSweetAlert(session = session)
  sendSweetAlert(
    session = session,
    title =" Calculation completed !",
    type = "success"
  )
})

}

shinyApp(ui = ui, server = server)
Tonio Liebrand
  • 17,189
  • 4
  • 39
  • 59
  • Works perfect! Thanks BDS! Small question - i am experimenting with the time rendered (sentences) by adjusting `Sys.sleep` but that (as you nkow) only prolongs the completion of the "Alert". Any ideas on how to get each to stay around 3s? – OctoCatKnows Apr 03 '19 at 22:20
  • It depends of courseon the calculation time of the function you call. You could at least approximate it. I think we should open a new question for that. – Tonio Liebrand Apr 03 '19 at 22:33
  • Indeeed-o. Im going to run a couple of functions through it to see what it looks like. Thanks again! – OctoCatKnows Apr 03 '19 at 22:45