Ralph Asher
Ralph Asher

Reputation: 226

Shiny: Display console output during specific function

I have a Shiny app that, upon clicking an observeEvent button, calls a function that outputs useful information to the console. I'd like to be able to show the console contents to the user, but only when this particular function is running. So in pseudocode, in my server it would show:

start_displaying_console()

myoutput <- complex_function_that_takes_awhile_with_important_console_info(arg1="hello",arg2="goodbye")

end_displaying_console()

Any help is appreciated.

Upvotes: 1

Views: 1690

Answers (1)

lz100
lz100

Reputation: 7360

You can use shinyCatch from spsComps

Example for your case:

library(shiny)
library(spsComps)
ui <- fluidPage(
    actionButton("a", "console messages"),
    actionButton("b", "console warnings"),
    actionButton("c", "console errors")
)

my_long_func1 <- function(){
    message("some msg")
}
my_long_func2 <- function(){
    warning("some warnings")
}
my_long_func3 <- function(){
    stop("some errors")
}

server <- function(input, output, session) {
    observeEvent(input$a, {
        spsComps::shinyCatch({
            my_long_func1()
        },
        # blocking recommended
        blocking_level = "error",
        prefix = "My-project" #change console prefix if you don't want "SPS"
        )
    })
    observeEvent(input$b, {
        spsComps::shinyCatch({
            my_long_func2()
        },
        # blocking recommended
        blocking_level = "error",
        prefix = "My-project" 
        )
    })
    observeEvent(input$c, {
        spsComps::shinyCatch({
            my_long_func3()
        },
        # blocking recommended
        blocking_level = "error",
        prefix = "My-project"
        )
    })
    
}

shinyApp(ui, server)

read docs and try more demos here

also in my another answer: R Shiny, shinyapps.io printing error messages for R codes

Upvotes: 1

Related Questions