首页 > 解决方案 > 进度条可以渲染随机句子吗?

问题描述

我正在测试从列表中生成随机句子并将其呈现在progressSweetAlert. 到目前为止,除了将第一个随机选择的句子发布为“价值”对象之外,我还无法取得进一步的进展。

我想要实现的是,随着进度条......的进展,随机选择的句子呈现几秒钟,然后继续到下一个字符串......例如......

“吃虫子……” “看着油漆变干……” “想大想……”

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

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

但是,这就是我得到的:

进展1 进展2

标签: rshiny

解决方案


也许你可以只使用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)

推荐阅读