Trouble using promises with shiny

101 views Asked by At

Suppose I have a shiny app that has an operation that takes a few seconds and the site is busy. On a multi-core machine I would like to take advantage of these cores so that I minimize user waiting time. I was thinking the promises library could help here.

My current synchronous server code looks something like this:

library(shiny)

observeEvent(input$button_press, {
  
  c(var1, 
    var2, 
    var3, 
    var4,
    var5) %<-% expensive_operation(
      arg1,
      arg2,
      arg3)
  
  # Draw graph --------------------------------------------------------------
  output$cc_plot <- renderPlot({
    cc_plot(var1)
  })
}

A user enters some info on a web page and presses a button. Calculations ensue and zeallot parcels them out into separate variables from a returned list.

Based on this, I tried the following to make it asynchronous:

library(future, exclude = c("%->%", "%<-%"))
library(promises)
library(zeallot)
library(shiny)

observeEvent(input$button_press, {
  
  # Draw graph --------------------------------------------------------------
  output$cc_plot <- renderPlot({
    
    c(var1, 
      var2, 
      var3, 
      var4,
      var5) %<-% future_promise(expensive_operation(
        arg1,
        arg2,
        arg3)) %...>%
    
    cc_plot(var1)
  })
}

This results in the error, object 'var1' not found

My understanding is that the use of the 'promise pipe' would prevent cc_plot() being called before var1 was known.

Any ideas on how to translate my synchronous code into asynchronous are appreciated.

0

There are 0 answers