进度栏可以呈现随机句子吗?

时间:2019-04-03 20:47:49

标签: r shiny

我正在测试从列表生成随机句子并将其呈现在progressSweetAlert中的可行性。到目前为止,除了将第一个随机选择的句子发布作为“值”对象之外,我无法走得更远。

我想要实现的是,随着进度栏...的进展,随机选择的句子会渲染几秒钟,然后继续执行下一个字符串...例如...

“正在吃虫子...” “看着油漆干...” “思想大思想。”

使用LaF程序包,我成功创建了一个sendencs列表并将其命名为:

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")

根据BDS的主要指导,这是一个有效的示例:):

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)

我希望能获得与这些示例(https://blog.teamtreehouse.com/make-loading-screen-unity)相似的东西。

但是,这就是我得到的:

Progress1 Progress2

1 个答案:

答案 0 :(得分:1)

也许您可以仅使用title参数?

您设置了title = sentences[sample(length(sentences), 1)],

updateProgressBar可能显示为:

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

完整示例如下:

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)