analyticalpicasso
analyticalpicasso

Reputation: 1993

Display R console logs on shiny server

I am building shiny app for data analysis operations. Which is all working fine.

I want to know that is there any way to display logs i.e whats happening behind in R Studio. Like print() messages or whatever R console is printing. I need to display all this activity interactively in the shiny app.

Like when we print progress, is there any way to append progress messages instead of displaying new message.

I have searched on interned but unable find anything in this regard.

Has anyone done this kind of thing ? Any kind help can be appreciated.

Upvotes: 9

Views: 11239

Answers (2)

Ferroao
Ferroao

Reputation: 3066

Related answer for checking log of a function with several messages for example.

library(shiny)

ui <- fluidPage(
  titlePanel("produce output or message"),
  sidebarLayout(
    sidebarPanel(
      radioButtons(inputId = "object",
                   label = "Generate output (or message)",
                   choices = c("cars", "iris")
                   ),
      radioButtons(inputId = "type",
                   label = "Type of capture",
                   choices = c("message", "output")
                   ,selected = "output"
      ),
    ),
    mainPanel(
      uiOutput("main")
      )
    )
)


server <- function(input, output, session) {
  
  values<-reactiveValues()
  
  observeEvent(input$object,{
    filename <- tempfile(fileext=".txt")
    filenamePath <- normalizePath(filename, mustWork = F)
    values[["outfile"]] <- filenamePath
  })
  
  observeEvent(c(input$object,input$type),{
  capture.output(
    # FUNCTION OF INTEREST
    get(input$object)
    ,file= (outfile <- file(values[["outfile"]],"w"))
    ,type=input$type
  )
  close(outfile)
  message(values[["outfile"]]) # for console only
  })
  
  filenameR <- eventReactive(c(input$object, input$type),{
    f<-values[["outfile"]]
  })
  
  output$log <- renderText({
    rawText <- filenameR()
    validate(need(try(readLines(rawText) ), message = FALSE) )
    replacedText <- paste(readLines(rawText), collapse = "\n")
    replacedText <- gsub("\\\033\\[[[:digit:]]{2}m","",replacedText) # removes ansicolor when present
    return(replacedText)
  }
  )
  
  output$main <- renderUI({     
    wellPanel(
    h2("log")
    ,verbatimTextOutput("log")
  ) 
  }) 
}

shinyApp(ui = ui, server = server)

enter image description here

Upvotes: 0

irJvV
irJvV

Reputation: 912

Well there is probably a better way that works for R & R Shiny on your computer & R Shiny that runs on the Server -> library(log4r)

library(log4r)
loggerDebug <- create.logger()
logfile(loggerDebug) <- 'data/debugData.log'
level(loggerDebug) <- 'INFO'

loggerServer <- create.logger()
logfile(loggerServer) <- 'data/serverData.log'
level(loggerServer) <- 'INFO'

# examples of levels
# debug(logger, 'A Debugging Message') # Won't print anything
# info(logger, 'An Info Message')
# warn(logger, 'A Warning Message')
# error(logger, 'An Error Message')
# fatal(logger, 'A Fatal Error Message')

Be sure to have the correct read and write acces on the server else it will not work. (remember that the R Server is writing and not you)

# this depends on your security settings and rights
# talk to your UNIX ADMIN first
test <- list()
test$test <- "test"

# to change in linux / unix 
system("chmod a+rwx /...pathToYourApp..../data")
system("chmod a+rwx /...pathToYourApp..../data/debugData.log")

info(loggerDebug, paste('|   TEST   |',test$test,"|"))

# close after write (for security):
system("chmod u=-x,g-x,o-rwx /...pathToYourApp..../data")
system("chmod u=-x,g-x,o-rwx /...pathToYourApp..../data/debugData.log")

To be even more secure you can do:

system("chattr +a /...pathToYourApp..../data/debugData.log")

This only allows appending to the file so no modifications can be made to the existing content. (can help to convince the UNIX ADMIN)

You can open the log file while you are working, be sure to refresh or reopen the file if you use RStudio or use a more dynamic package that updates itself (like Sublime Text or .... )

I hope this is of help, perhaps you found an even better way, let us know

Upvotes: 9

Related Questions