英文:
How do I display a shinyalert pop-up while loading data in R shiny?
问题
我正在处理一个相当“大”的数据集,需要至少20秒才能在启动我的Shiny应用程序时完全读取。我想在这个读取时间内显示一个弹出式等待消息,就像下面这样,当读取完成时,它会自动关闭。
然而,我不知道如何指定一个条件来自动关闭警报。
到目前为止,我已经完成了以下工作:
server <- function(input, output, session) {
data =read_sav("BS.SAV")
shinyalert(
title = "Wait",
text = "Waiting for data loading",
size = "xs",
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
html = TRUE,
type = "info",
showConfirmButton = TRUE,
confirmButtonText = "OK",
confirmButtonCol = "#004192",
showCancelButton = FALSE,
imageUrl = "",
animation = TRUE
)
}
英文:
I'am working on a pretty "big" dataset that takes at least 20 seconds to be read entirely at the launch of my Shiny app. I would like to display a pop-up waiting message like the one below during this reading time, which will close automatically when it is done.
However, I have no idea how to specify a condition to automatically close the alert.
Below what I have done so far :
server <- function(input, output, session) {
data =read_sav("BS.SAV")
shinyalert(
title = "Wait",
text = "Waiting for data loading",
size = "xs",
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
html = TRUE,
type = "info",
showConfirmButton = TRUE,
confirmButtonText = "OK",
confirmButtonCol = "#004192",
showCancelButton = FALSE,
imageUrl = "",
animation = TRUE
)
}
答案1
得分: 1
以下是翻译好的部分:
这是一个示例。可以使用 CloseAlert()
来关闭警报。.csv
是一个reactive
,由 fileInput
读取。我们使用自定义的点击处理程序来检查是否点击了 fileInput
按钮。然后,输出最终呈现在 DT::datatable
中,当该对象最终呈现时,警报将关闭。
library(shiny)
library(shinyalert)
options(shiny.maxRequestSize = 30 * 1024 ^ 2 * 2)
js <- HTML(
"
$(function() {
$('.shiny-input-container .input-group-btn .btn').on('click', function() {
const id = $(this).find('input[type=\"file\"]').attr('id');
Shiny.setInputValue(id + '_click', Math.random());
})
})"
)
shinyApp(
ui = fluidPage(
singleton(tags$head(tags$script(js))),
fileInput(
inputId = 'file',
label = '选择CSV文件',
accept = c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')
),
DT::dataTableOutput("table")
),
server = function(input, output, session) {
input_file <- reactive({
if (is.null(input$file)) {
return("")
}
read.csv(file = input$file$datapath)
})
output$table <- DT::renderDataTable({
req(input_file())
closeAlert()
input_file()
})
observeEvent(input$file_click,
{
shinyalert(
title = "等待",
text = "等待数据加载",
size = "xs",
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
html = TRUE,
type = "info",
showConfirmButton = TRUE,
confirmButtonText = "确定",
confirmButtonCol = "#004192",
showCancelButton = FALSE,
imageUrl = "",
animation = TRUE
)
},
ignoreNULL = FALSE,
ignoreInit = TRUE)
}
)
英文:
Here is one example. The alert can be closed using CloseAlert()
. The .csv
is a reactive
which gets read by a fileInput
. We use a custom click handler in order to check whether the fileInput
button gets clicked. The output gets then rendered into a DT::datatable
and when this object is finally rendered, the alert gets closed.
library(shiny)
library(shinyalert)
options(shiny.maxRequestSize = 30 * 1024 ^ 2 * 2)
js <- HTML(
"
$(function() {
$('.shiny-input-container .input-group-btn .btn').on('click', function() {
const id = $(this).find('input[type=\"file\"]').attr('id');
Shiny.setInputValue(id + '_click', Math.random());
})
})"
)
shinyApp(
ui = fluidPage(
singleton(tags$head(tags$script(js))),
fileInput(
inputId = 'file',
label = 'Choose CSV File',
accept = c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')
),
DT::dataTableOutput("table")
),
server = function(input, output, session) {
input_file <- reactive({
if (is.null(input$file)) {
return("")
}
read.csv(file = input$file$datapath)
})
output$table <- DT::renderDataTable({
req(input_file())
closeAlert()
input_file()
})
observeEvent(input$file_click,
{
shinyalert(
title = "Wait",
text = "Waiting for data loading",
size = "xs",
closeOnEsc = TRUE,
closeOnClickOutside = TRUE,
html = TRUE,
type = "info",
showConfirmButton = TRUE,
confirmButtonText = "OK",
confirmButtonCol = "#004192",
showCancelButton = FALSE,
imageUrl = "",
animation = TRUE
)
},
ignoreNULL = FALSE,
ignoreInit = TRUE)
}
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论