I would like to continuously update and display a process log file in my Shiny app, please find replex below.
However, the log file is only displayed after the process is completed and not every 500 ms as I specified.
How can I let the app perform reactiveFileReader and the time intensive computational process in parallel?
slow.function = function() {
# this function is not part of the shiny app,
# it may be inside another R package
# or even a System command using non-R software.
# so you cannot edit it.
for (i in 1:10) {
cat(paste0("status = ", i, "\n"))
Sys.sleep(0.5)
}
}
ui <- fluidPage(
h3("My process log:"),
htmlOutput("mylog")
)
server <- function(input, output, session) {
# create temp log file
logfile_tmp <- tempfile(fileext = ".log")
# send temp log to UI twice per second
mylog <- reactiveFileReader(500, NULL, logfile_tmp, readLines)
output$mylog <- renderUI({
HTML(paste(mylog(), collapse = '<br/>'))
})
# fill temp log file with the console output of some time intensive computation
con <- file(logfile_tmp)
sink(con, append = TRUE)
sink(con, append = TRUE, type = "message")
# start time intensive computation
slow.function()
sink()
sink(type = "message")
}
shinyApp(ui, server)