Is it possible to stop executing of R code inside shiny (without stopping the shiny process)?

前端 未结 5 836
夕颜
夕颜 2020-12-03 03:26

Let\'s say I have a shiny app that has a function that can take a long time to run. Is it possible to have a \"stop\" button that tells R to stop the long-running call, wit

5条回答
  •  一个人的身影
    2020-12-03 03:59

    What about httpuv::service() ?

    library(shiny)
    analyze <- function(session=shiny::getDefaultReactiveDomain()){
      continue = TRUE
      lapply(1:100, function(x) {
        if(continue){
          print(x)
          Sys.sleep(1)
          # reload inputs
          httpuv:::service()
          continue <<- !isTRUE(session$input$stopThis)
        }
      }
      )
    }
    
    shinyApp(
      ui = fluidPage(
        actionButton("start","Start",class="btn-primary", onclick="Shiny.onInputChange('stopThis',false)"),
        actionButton("stop","Stop",class="btn-danger", onclick="Shiny.onInputChange('stopThis',true)")
      ),
      server = function(input, output, session) {
        observeEvent(input$start, {
          analyze()
        })
      }
    )
    

提交回复
热议问题