Here you can find out why promises::future_promise
might not be what you are searching for in this context (intra-session vs. inter-session responsiveness).
I'd propose using callr::r_bg
(for async R processes) or processx::process
(all processes - r_bg
is based on processx
) in this situation.
Setting reactiveFileReader's session
parameter to NULL
suggests that you want to create a cross-session file reader - this needs to be done outside of the server function. Please see my related post here.
library(shiny)
library(callr)
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)
}
}
# create temp log file
logfile_tmp <- tempfile(fileext = ".log")
# send temp log to UI twice per second
mylog <- reactiveFileReader(500, NULL, logfile_tmp, readLines)
ui <- fluidPage(
h3("My process log:"),
htmlOutput("mylog")
)
server <- function(input, output, session) {
output$mylog <- renderUI({
HTML(paste(mylog(), collapse = '<br/>'))
})
r_bg(func = function(slow.function){print(slow.function())}, args = list(slow.function), stdout = logfile_tmp, stderr = "2>&1")
# or directly use processx::process instead of callr::r_bg
print(logfile_tmp)
}
shinyApp(ui, server)