I have come across an interesting example of the following R-Shiny code:
missing_files_exist <- check_for_missing_files()
if (missing_files_exist) {
shinyWidgets::show_alert(
title = "Missing Files Detected",
text = "It appears file x is missing.",
type = "warning"
)
}
if (nrow(data) == 0) {
shinyWidgets::sendSweetAlert(
title = "No data",
text = "Please load data first.",
type = "error"
)
return(NULL)
}
In this case the check_for_missing_files function is very elaborate, taking around 2 seconds to complete. This leads to the "No data" alert being shown first and, thus, suppressing the "Missing Files" alert. This seems odd to me, since I expected R to move on to the next line only once the first one has finished. I am sure that is what happening in the background but then I struggle to find an explanation for this behavior.
Adding a sleep statement fixes the issue, in case that is of relevance:
missing_files_exist <- check_for_missing_files()
if (missing_files_exist) {
shinyWidgets::show_alert(
title = "Missing Files Detected",
text = "It appears file x is missing.",
type = "warning"
)
}
Sys.sleep(2)
if (nrow(data) == 0) {
shinyWidgets::sendSweetAlert(
title = "No data",
text = "Please load data first.",
type = "error"
)
return(NULL)
}
Question: Why does this behavior happen and what can I do to prevent it?