r - 进度条可以渲染随机句子吗?
问题描述
我正在测试从列表中生成随机句子并将其呈现在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)。
但是,这就是我得到的:
解决方案
也许你可以只使用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)
推荐阅读
- c# - 如何在 C# 的纸牌游戏中决定玩家和计算机之间的轮换?
- python - 如何在python中将整个对象发送到火花地图?
- rust - 为什么将 match 语句包装为 Ok 被认为是错误的形式?
- haskell - 为什么我无法导入模块?
- ios - IOS 应用程序在强制关闭后收到 pushkit voip 通知时崩溃
- matlab - 创建一个包含变量名称单元格和双精度数组单元格的结构
- sql - 在 Oracle 中插入带单引号或撇号的列
- angular - 来自其他组件的 Angular @Input() 数组,问题
- machine-learning - 反向传播示例
- kaggle - kaggle 直接从复制的内核下载输入数据