3

I have created an application using R and Shiny and want to output everything that happens in the console to a special status window in the Shiny app. Here is how the skeleton of the function called by Shiny application looks.

myfunction = function(x,y,path....){
if(...){...}
cat("Reading Database\n")
df = read.csv(...)
cat("Processing\n")
#MORE CODE
}

I want a status bar that shows the progress of the called function by viewing the cat(...) console outputs. If yes, can it be done without making any changes to the original function (making changes only in server.R and/or ui.R)?

2
  • Does it have to be cat and not message? If you can use message then I have a solution. If it uses cat then I only know how to make all the output appear at once when the function finishes
    – DeanAttali
    Jul 24, 2015 at 23:08
  • Actually I didn't know about message function. I would appreciate to see your solution with message. Thanks!
    – dbudaghyan
    Jul 24, 2015 at 23:13

2 Answers 2

1

Unfortunately I don't know how to do this with the normal Shiny approach of using reactivity. I tried getting it to work with textOuput+printText but I was unable. I'd love to see other solutions, but here is my solution that uses shinyjs package to update the element instead of using reactivity. I hope this works for you, it's pretty simple.

library(shiny)
library(shinyjs)

calculate <- function() {
  lapply(1:5, function(x) {
    message(x)
    Sys.sleep(0.5)
  })
  message("Done")
}

runApp(shinyApp(
  ui = fluidPage(
    shinyjs::useShinyjs(), br(),
    actionButton("btn","Click me"), br(), br(),
    "Progress:",
    tags$pre(id = "progress")
  ),
  server = function(input,output, session) {
    observeEvent(input$btn, {
      withCallingHandlers({
        shinyjs::text("progress", "")
        calculate()
      },
      message = function(m) {
        shinyjs::text(id = "progress", text = m$message, add = TRUE)
      })
    })
  }
))
1
  • 1
    Hi Dean, I am using my function() to extract data and it is used inside an eventReactive function to make the dataset reactive with button click. How can I use your code structure in that case? Mar 15, 2018 at 2:53
0

Ran across this answer that may be what you are looking for.

The withConsoleRedirect() function sends the console output to the browser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.