r - R Shiny - 如何在不停止应用程序的情况下打破应用程序?
问题描述
下面的应用程序包含一个在单击时actionButton
触发的lapply
。lapply
循环通过数字 2-4 并在 x %% 2 不为 0 时停止。是否可以在不lapply
停止主应用程序的情况下中断?
library(shiny)
ui <- fluidPage(
actionButton(inputId = "go", label = "Start"),
div(id = 'placeholder')
)
server <- function(input, output, session) {
observeEvent(input$go, {
lapply(2:4, function(x) {
res = x %% 2
if(res == 0){
return(x)
} else {
insertUI('#placeholder', ui = tags$p('There was an error.'))
stop('Error')
}
})
})
}
shinyApp(ui = ui, server = server)
req
x %% 2 == 0
不是一个选项,因为如果不满足条件,我需要在终止循环之前插入一些 UI 。
我在这里发现了一个类似的问题:Is it possible to stop execution of R code inside shiny (without stop the shiny process)? . 但它依赖于用户输入来停止执行,我不知道如何将它修改为这个例子。我也无法尝试修改它,因为parallel
它不适用于 R 版本 3.6.0。我还看到了这里引用的这篇文章:https ://github.com/rstudio/shiny/issues/1398但我认为它也需要用户输入。
解决方案
根据您应用程序中的确切要求,也许您可以使用“正常”循环并使用break
来停止循环执行。或者,您可以将其包装在一个try
调用中:
server <- function(input, output, session) {
observeEvent(input$go, {
try(lapply(2:4, function(x) {
res = x %% 2
if(res == 0){
return(x)
} else {
insertUI('#placeholder', ui = tags$p('There was an error.'))
stop('Error')
}
}), silent=T)
})
}
推荐阅读
- javascript - PostgreSQL:将日期与过程中的 if else 进行比较
- python-3.x - 如何在python中打印带有特定字符串的行?
- python - 在 kivy 中动态地将项目添加到滚动视图时出现 font_size 错误
- go - 无法获取请求标头
- postgresql - 邮递员。使用参数值作为列标识符
- python - Pandas - 如何使用另一个数据框从数据框中提取列?
- python - 没有for循环的时间复杂度
- microsoft-teams - 在 microsoft 团队自定义应用程序中获取 iframe 中加载的最后一个 url
- flutter - 为什么我们不应该直接使用 `pub` 命令?
- mongodb - typegoose 中枚举的建模列表