【问题标题】:How to start and stop invalidateLater function如何启动和停止 invalidateLater 函数
【发布时间】:2018-04-25 17:47:07
【问题描述】:

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

【问题讨论】:

    标签: r shiny


    【解决方案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)
        })
      }
    )
    

    【讨论】:

    • 谢谢。使用复选框的另一个有趣的解决方案。谢谢。
    【解决方案2】:
    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,条件变为假。

    另请参阅此答案: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。

    【讨论】:

    • @Aure'le,谢谢。 “停止”按钮有效,但是当我再次单击“开始”按钮时,它不再自动运行。此外,您能否仅在第一次按下“开始”按钮时才运行计数。谢谢。
    猜你喜欢
    • 2012-01-22
    • 2012-08-16
    • 2012-07-04
    • 2012-07-31
    • 2018-06-12
    • 2021-06-30
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    相关资源
    最近更新 更多