I recently ran into a problem that as my computing took lots of time, I'd like to show the text output on shiny rather than progress bar or loading message. My function looked like
printText <- function() {
for(i in 1:10){
Sys.sleep(0.1)
print(paste("My text", i))
y = i + 1
}
return(y)
}
I can print it with verbatimTextOutput
but I also need the returned value of y. Now I am doing this:
runApp(list(
ui = shinyUI(fluidPage(
titlePanel("Print consol output"),
sidebarLayout(
sidebarPanel(actionButton("go", "Go")),
mainPanel(
verbatimTextOutput("text")
)
)
)),
server = shinyServer(function(input, output, session){
observeEvent(input$go, {
output$text <- renderPrint({
y <- printText()
})
})
})
))
The problem is that if I want to use the returned y I need to create a reactive
object, which may take me 2 times longer because I execute printText()
twice, while printing and pass to reactive
object.
How could I get the value of y and print the text onto shiny without duplicated work? Notice that I'm not gonna use progress bar because my real function is not loop actually. What I want is to capture the text output during the process and get returned value.