Can we transfer message to javascript asynchronously with session$sendCusomMessage()?

,

Hi,

I found the session$sendCustomMessage will also block the main thread when transferring some "large data" from R to javascript. I would like to ask if we could make it asynchronously? I found that shiny for python supports "async" method, but have not find a way in shiny for R. Any suggestion would be great appreciated!

Thanks.

blocked_app.R modified from ExtendedTask example:

library(shiny)
library(bslib)

ui <- page_fluid(
  p("The time is ", textOutput("current_time", inline=TRUE)),
  hr(),
  numericInput("x", "x", value = 1),
  numericInput("y", "y", value = 2),
  input_task_button("btn", "Add numbers"),
  textOutput("sum")
)

server <- function(input, output, session) {
  large_df <- data.frame(
    id = 1:10000000,
    value1 = rnorm(10000000),
    value2 = runif(10000000),
    category = sample(LETTERS, 10000000, replace = TRUE)
  )
 
  output$current_time <- renderText({
    invalidateLater(1000)
    format(Sys.time(), "%H:%M:%S %p")
  })

  sum_values <- eventReactive(input$btn, {
    ##Sys.sleep(5)
    ##input$x + input$y
    session$sendCustomMessage(type = "custom", large_df)
  })

  output$sum <- renderText({
    sum_values()
  })
}

shinyApp(ui, server)

Please check ?shiny::ExtendedTask.

Thanks a lot for the information. I've tested ExtendedTask, and it seems that the session object could not be exported to the background thread, could you elaborate more instructions on transferring data with ExtenedTask?

Sorry, currently I don't have the time to elaborate or test something. If ExtendedTask doesn't work for you, you can try this alternative approach.

Thanks for the helpful threads. My current workaround is writing data to file in ExtendedTask instance, with unique filename, and use sendCustomMessage to ask javascript to fetch the file when it's ready.

This topic was automatically closed 90 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.