R 如何启动和停止失效关联器功能

R 如何启动和停止失效关联器功能,r,shiny,R,Shiny,我是新的R和闪亮,我想写一个应用程序,将自动打印,我可以按停止,如果我想改变设置。我找到了一个简单的例子,当我点击Run或Stop按钮时,我试图进行修改,但没有成功。有人能给我看一下我的问题或者一些我可以学习的文件吗。多谢各位 library(shiny) library(shinyjs) shinyApp( ui = fluidPage( useShinyjs(), # Set up shinyjs "Count:", textOutput("n

我是新的R和闪亮,我想写一个应用程序,将自动打印,我可以按停止,如果我想改变设置。我找到了一个简单的例子,当我点击Run或Stop按钮时,我试图进行修改,但没有成功。有人能给我看一下我的问题或者一些我可以学习的文件吗。多谢各位

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")
      })
    }
  )
这是一种解决方法,因为无法重置操作按钮。

解决方案是使用复选框输入停止按钮:

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

@奥勒,谢谢你。停止按钮起作用了,但当我再次单击开始按钮时,它不再自动运行。另外,当第一次按下“开始”按钮时,您可以只运行计数。谢谢,谢谢。另一个有趣的解决方案是使用复选框。谢谢
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)
    })

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