如何将阻止功能包装到Promise RShiny中 [英] How do I wrap a blocking function into a promise RShiny

查看:69
本文介绍了如何将阻止功能包装到Promise RShiny中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个R Shiny仪表板,其中有2个设置为在特定时间刷新的观察者,一个观察者每6小时刷新一次,其他观察者每2分钟刷新一次.两个观察者都运行一个返回反应性值的函数.这可以正常工作,但是每第6个小时触发第一个观察者时,它就会锁定仪表板并阻止其他观察者正常工作.经过一番阅读后,我知道我需要使用期货和承诺,但是无法实现任何可以正常使用的东西.如何将每个观察器中的功能包装到各自的将来以防止阻塞?

I have an R Shiny dashboard that has 2 observers that are set to refresh at specific times, One observer refreshes every 6 hours, the other every 2 mins. Both observers run a function that returns a reactive value. This works fine, however every 6 hours when the first observer is triggered it locks the dashboard and prevents the other observer from functioning. After some reading I know that I need to use futures and promises but am unable to implement anything that works as intended. How do I wrap the functions in each observer into respective futures that would prevent blocking?

  values <- reactiveValues()



  observe({

    # Re-execute this reactive expression every 2 mins

    invalidateLater(120000, session)

    values$twominresult <-  twoMinFunction()

  })


  observe({

    # Re-execute this reactive expression every 6 hours

    invalidateLater(21600000, session)

    values$sixhourresult <- sixhourfunction()

  })

推荐答案

以下是基于您的代码段的会话内非阻塞期货的示例:

Here is an example for intra-session non-blocking futures based on your code snippets:

library(shiny)
library(promises)
library(future)
plan(multiprocess)

twoMinFunction <- function(){
  return(Sys.time())
}

sixHourFunction <- function(){
  Sys.sleep(3)
  return(Sys.time())
}


server <- function(input, output, session) {

  values <- reactiveValues(twominresult = NULL, sixhourresult = NULL)

  observe({
    # Re-execute this reactive expression every 2 seconds # mins
    invalidateLater(2000, session) # 120000

    myTwoMinFuture <- future({
      twoMinFunction()
    })

    then(myTwoMinFuture, onFulfilled = function(value) {
      values$twominresult <- value
    },
    onRejected = NULL)

    return(NULL)
  })


  observe({
    # Re-execute this reactive expression every 6 seconds # hours
    invalidateLater(6000, session) # 21600000

    mySixHourFuture <- future({
      sixHourFunction()
    })

    then(mySixHourFuture, onFulfilled = function(value) {
      values$sixhourresult <- value
    },
    onRejected = NULL)

    return(NULL)
  })

  output$twominout <- renderText({
    paste("two min result:", values$twominresult)
  })

  output$sixhoursout <- renderText({
    paste("six hour result:", values$sixhourresult)
  })

}

ui <- fluidPage(textOutput("twominout"),
                textOutput("sixhoursout"))

shinyApp(ui, server)

我把它加快了一点,所以您可以看到更改.

I made it a little faster, so you can see the changes.

请注意observeEvent()中的return(NULL)-这在其自己的会话中隐藏了未来-允许会话内响应.请记住,如果使用错误的方式,此模式可能会导致竞争状况(请参阅Joe Cheng的

Please note the return(NULL) in the observeEvent() - this is hiding the future from its own session - allowing intra-session responsiveness. Please keep in mind that this pattern may cause race conditions if used the wrong way (Please see Joe Cheng's comment, which I already mentioned above)

这篇关于如何将阻止功能包装到Promise RShiny中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆