[英]How to start and stop invalidateLater function
I am new to R and Shiny, I'd like to write an application which will plot automatically and I can press to stop if I want to change a setup. 我是R和Shiny的新手,我想编写一个将自动绘制的应用程序,如果要更改设置,可以按停止。 I found a simple example, and I tried to modify when I clicked "Run" or "Stop" button but it didn't work. 我找到了一个简单的示例,当我单击“运行”或“停止”按钮时,我尝试进行修改,但是没有用。 Could someone please show my issue or some documents that I can learn to do it. 有人可以告诉我我的问题或一些我可以学习的文件。 Thank you. 谢谢。
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(),
# Set up shinyjs
"Count:", textOutput("number", inline = TRUE), br(),
actionButton("start", "Start"), br(),
"The button will be pressed automatically every 3 seconds",br(),
actionButton("stop", "Stop"), br(),
"The counter will stop when the button is pressed"
),
server = function(input, output) {
output$number <- renderText({
input$start
})
observe({
#if (click("start") == TRUE) {
click("start")
invalidateLater(3000)
# }
})
observe({
click("stop")
#shinyjs::disable("start")
})
}
)
Solution is to use a checkboxInput
for the stop button: 解决方案是对停止按钮使用checkboxInput
:
library(shiny)
library(shinyjs)
shinyApp(
ui = fluidPage(
useShinyjs(),
# Set up shinyjs
"Count:", textOutput("number", inline = TRUE), br(),
actionButton("start", "Start"), br(),
"The button will be pressed automatically every 3 seconds",br(),
checkboxInput("stop", "Stop"), br(),
"The counter is stopped when the checkbox is pressed"
),
server = function(input, output, session) {
output$number <- renderText({
input$start
})
# unselect stop when start is pressed
observeEvent(input$start, {
if(input$stop){
updateCheckboxInput(session, "stop", value = FALSE)
}
})
# every 3000 ms, press start (if stop is unselected, else do nothing)
observe({
invalidateLater(3000)
if(!isolate(input$stop)){
click("start")
updateCheckboxInput(session, "stop", value = FALSE)
}
})
# after clicking start, uncheck stop checkbox
observeEvent(input$start, {
updateCheckboxInput(session, "stop", value = FALSE)
})
}
)
shinyApp(
ui = fluidPage(
useShinyjs(),
"Count:", textOutput("number", inline = TRUE), br(),
actionButton("start", "Start"), br(),
"The button will be pressed automatically every 3 seconds",br(),
actionButton("stop", "Stop"), br(),
"The counter will stop when the button is pressed"
),
server = function(input, output) {
observe(cat(str(reactiveValuesToList(input)), "\n"))
output$number <- renderText({
input$start
})
observe({
if (!input$stop) {
click("start")
invalidateLater(3000)
}
})
}
)
observe(cat(str(reactiveValuesToList(input)), "\\n"))
is a "shiny dev trick" of mine to help me see what's happening. observe(cat(str(reactiveValuesToList(input)), "\\n"))
是我的“闪亮开发技巧”,可以帮助我了解发生了什么。 input$stop
is initially 0
. input$stop
最初为0
。 The first time it's clicked, it's incremented to 1
and the condition becomes false. 第一次单击时,它增加到1
,条件变为假。
See also this answer: https://stackoverflow.com/a/47486524/6197649 另请参阅以下答案: https : //stackoverflow.com/a/47486524/6197649
Edit per your comment: 根据您的评论进行编辑:
server = function(input, output, session) {
output$number <- renderText({
input$start
})
stop_2 <- reactiveVal(FALSE)
observeEvent(input$stop, stop_2(TRUE))
observeEvent(input$start, if (stop_2()) stop_2(FALSE))
observe({
if (isolate(!stop_2() && input$start)) click("start")
invalidateLater(3000)
})
}
This is a workaround because of the impossibility to reset an actionButton. 这是一种解决方法,因为无法重置actionButton。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.