Show messages from plumber API in shiny APP

20 Views Asked by At

I have API in plumber which has msg endpoint which executes slow_function. I also have shiny APP in which I make msg endpoint request. Is it possible to somehow capture messages from API and display them in shiny APP?

slow function:

slow_function <- function() {
  
  messages <- character(0)
  for (i in 1:5){
    message(paste("Step:", i))
    Sys.sleep(2)  
  }
  
 TRUE
}

I could capture messages with withCallingHandlers if slow_function runs in APP but as soon as I replace with request to the API it does not work.

APP

library(shiny)
library(waiter)
library(httr)

ui <- fluidPage(
  
  waiter::useWaiter(),
  
  actionButton(
    inputId = "run_fun",
    label = "Run Function"
  )
)

server <- function(input, output, session){
  
  observeEvent(input$run_fun, {
    
    total_process <- character(0)
    my_waiter <- waiter::Waiter$new()
    
    my_waiter$show()
    on.exit(my_waiter$hide())
    
    withCallingHandlers(
      slow_function(), #"httr::GET("http://localhost:8000/msg"),
      message = function(m){
        total_process <<- paste(total_process, m$message, sep = "<br>")
        my_waiter$update(HTML(total_process))
      }
    )
  })
}

shinyApp(ui, server)

API:

library(plumber)

#* Run Slow Function with Messages
#* @get /msg
function(){
  slow_function()
}

## Run this to run api
# pr <- plumb("plumber.R")
# pr$run(port=8000)

Is there some way to display messages from running function within API in APP? This is important to me becaue my slow_function is within R package which has dependencies that I dont want to have on frontend and this step display would drastically improve user experience.

0

There are 0 best solutions below