Capture console output for Shiny apps
brianwdavis opened this issue · 2 comments
I used {progress} in a package, because I expect it to mostly be used in interactive sessions. However, I want to make a demo with Shiny for an upcoming conference, and I don't see a good way to capture the printed bars and show them in the app.
I tried using solutions like https://stackoverflow.com/questions/30474538/possible-to-show-console-messages-written-with-message-in-a-shiny-ui, but only got [1] "\r"
printed in my HTML element.
I could rewrite my package to check for the context, and if it's Shiny instead of interactive()
, switch to using the Shiny-native progress bars, but that's... not the best solution. Are there any straightforward ideas I'm missing here?
Once the condition branch has been merged you'll be able to use something like in hadley/mastering-shiny#113 — I'll update https://mastering-shiny.org/action-feedback.html#progress-bars when it's ready to try out.
It's not great. But it works.
library(shiny)
library(progress)
foo <- function() {
pb <- progress_bar$new(
format = " [:bar] :current/:total (:percent) eta: :eta",
total = 5, clear = FALSE)
for(i in 1:5) {
pb$tick()
Sys.sleep(1)
}
}
ui <- fluidPage(
shinyjs::useShinyjs(),
actionButton("start_pb","Start Progress Bar"),
textOutput("pb")
)
server <- function(input, output) {
observeEvent(input$start_pb, {
withCallingHandlers({
shinyjs::html("pb", "")
foo()
},
message = function(m) {
shinyjs::html(id = "pb", html = m$message, add = FALSE)
})
})
}
shinyApp(ui = ui, server = server)