I am unable to create even a simple working implementation of asynchronous programming in Shiny

Hi all,

I am attempting to learn to use promises for asynchronous execution of code to handle a particularly long function that is locking up a large complicated app I've been working on.

As a practice before implementing it properly in my larger app, I've made a minimal app to try to test it and learn all the rules.. however even this super simplistic implementation is not working properly for me.

Clicking the slow increment button and then the fast increment button still waits the full 10 seconds before either output is updated. I have tried opening it in two different browser tabs as I saw recommended in thisthread: Async with Promises executing sequentially however it is still locking up both sessions.

Am I missing something obvious?

Here is my code = apologise in advance if it doesn't comethrough correctly, this is my first topic here and I don't see a preview button to check the formatting!

#> global.r
library(shiny)
library(future)
library(promises)

plan(multisession)

#> ui.r
fluidPage(
    fluidRow(
        actionButton("fastOutputButton", "Fast increment"),
        textOutput("fastOutput")
    ),
    fluidRow(
        actionButton("slowOutputButton", "Slow increment"),
        textOutput("slowOutput")
    )
)

#> server.r
function(input, output, session) {
  state <- reactiveValues(
    fastCount = 0,
    slowCount = 0
  )

  output$fastOutput <- renderText({
    state$fastCount
  })

  output$slowOutput <- renderText({
    state$slowCount
  })

  observeEvent(input$fastOutputButton, {
    state$fastCount <- state$fastCount + 1
  })

  observeEvent(input$slowOutputButton, {
    future({
      # Expensive code goes here
      Sys.sleep(10)
    }) %...>% (function(result) {
      # Code to handle result of expensive code goes here
      state$slowCount <- state$slowCount + 1
    })
  })
}

I posted this question on the rshiny subreddit as well, and one helpful user suggested that my processor may be single core.

Explicitly setting the number of workers to 2 solved the issue.

plan(multisession,workers = 2)

https://www.reddit.com/r/rshiny/comments/14kt68j/i_cant_get_even_the_most_simple_example_of/

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.