How to make an operation uninterruptible in R shiny

后端 未结 2 1441
挽巷
挽巷 2021-02-06 04:41

In my shiny app I have a output which should update itself continuously. But whenever I execute a long-running calculation, the output is just paused. My question is: how to mak

2条回答
  •  情书的邮戳
    2021-02-06 05:21

    Your problem is that Sys.sleep() suspends the execution of R expression. You can use things like the delay() function from shinyjs

    library(shiny)
    library(shinyjs)
    
    ui <- fluidPage(
      useShinyjs(), #You have to add this.
      actionButton("button","Expensive calcualtion(takes 5 seconds)"),
      tags$p("Current Time:"),
      textOutput("time"),
      tags$p("Result from clicking button:"),
      textOutput("result")
    )
    
    server <- function(input, output, session) {
      timer <- reactiveTimer(500)
      current_time <- reactive({
      timer()
      as.character(Sys.time())
     })
     output$time <- renderText(current_time())
    
     observeEvent(input$button,{
     delay(5000, output$result <- renderText("result"))
     })
    }
    
    shinyApp(ui, server)
    

提交回复
热议问题