繁体   English   中英

进度条可以渲染随机句子吗?

[英]Can a progress bar render random sentences?

我正在测试从列表生成随机句子并将其呈现在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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM