I'd like to show some long-computing function's status in shiny UI under the following situation.
(1). I am not allowed to edit inside of long-computing function
(2). long-computing function print their status into R console
Due to this restriction (1) , I think I can not use withProgress()
or something requires to put inside of the function. So I attempted to show R console output to shiny UI . Starting my app by R CMD BATCH myscript.R
and shinytail
can capture the update of logfile on the shiny UI.
My problem:
I can see update on logfile in shinyUI, but it is not realtime. In following code, logged print()
messages will appear in shiny ui after long.calculation function is finished despite of redirected logfile is updated realtime (I confirmed redirected logfile is updated realtime by tail -F logfile
in terminal).
How can I capture this long.calculation function's progress under this restrictions? I would appreciate any comments. Thanks!!
library(shiny)
library(shinydashboard)
library(shinytail)
options(shiny.port=3131, shiny.host='0.0.0.0')
## Restriction: I am not allowed to edit inside of long.calculation
long.calculation <- function(n){
for (i in seq(1, n)){
Sys.sleep(1)
print(sprintf('Some logging %s: this logging will not appear in shiny ui in realtime..', i))
}
}
server <- function(input, output, session) {
## ***************************
## To run this script, I use:
## > R CMD BATCH ./example2.R
## ***************************
logfile <- './example2.Rout'
all_data <- reactiveVal(value=NULL, label='data')
pr <- tailFile(logfile)
observe({
readStream(all_data, pr)
})
output$log <- renderText({
paste(all_data(), collapse='\n')
})
observeEvent(input$exec, {
n <- 30
long.calculation(n)
## withProgress(message = 'Long calculation status',
## detail = 'But this bar is start after long calculation is finished...', value = 0, {
## for (i in seq(1, n)) {
## incProgress(1/n)
## Sys.sleep(1)
## }
## })
})
}
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem('Exec & Log', tabName='execlog', icon=icon('th'), selected=TRUE)
)
)
body <- dashboardBody(
tabItem(tabName = 'execlog',
shinyTail('log'),
actionButton('exec', 'Exec')
)
)
ui <- dashboardPage(
dashboardHeader(title='example'),
sidebar,
body
)
runApp(shinyApp(ui, server))