如何启动和停止invalidateLater函数

时间:2018-04-25 17:47:07

标签: r shiny

我是R和Shiny的新手,我想写一个自动绘图的应用程序,如果我想更改设置,我可以按停止。我找到了一个简单的例子,当我点击“运行”或“停止”按钮时我试图修改但它没有用。有人可以出示我的问题或一些我可以学习的文件。谢谢。

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")
      })
    }
  )

2 个答案:

答案 0 :(得分:1)

解决方法是使用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)
    })
  }
)

enter image description here

答案 1 :(得分:0)

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"))是我的“闪亮的开发技巧”,可以帮助我了解正在发生的事情。 input$stop最初为0。第一次单击时,它会递增到1,条件变为false。

另见答案:https://stackoverflow.com/a/47486524/6197649

根据您的评论进行修改:

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)
    })

  }

这是一种解决方法,因为无法重置actionButton。