Search code examples
rshinyexecution-time

Measure execution speed inside of Shiny


I am working on developing a Shiny app.

I am interested in clocking the time it takes to execute certain chunks of code (such as a ggplot, etc).

For some reason it appears that using the usual clocking methods don't work within reactive calls, for example:

output$R1_C1 <- renderPlot({

beginning <- Sys.time()

<lots of code here> 

end <- Sys.time()
print(end - beginning)

R complains and gives me

Error in (structure(function (input, output)  : 
  object 'beginning' not found

Has anyone found a successful way to time execution speed inside of reactive calls in Shiny?


Solution

  • This works on my system:

    library(shiny)
    runApp(list(
      ui = bootstrapPage(
        numericInput('n', 'Number of obs', 100),
        plotOutput('plot')
      ),
      server = function(input, output) {
        output$plot <- renderPlot({
          beginning <- Sys.time()
          h <- hist(runif(input$n)) 
          end <- Sys.time()
          print(end - beginning)
          h
        })
      }
    ))