【问题标题】:Measure execution speed inside of Shiny测量 Shiny 内部的执行速度
【发布时间】:2014-12-27 04:41:56
【问题描述】:

我正在开发一个闪亮的应用程序。

我有兴趣计算执行某些代码块(例如ggplot 等)所需的时间。

由于某种原因,使用通常的计时方法似乎不适用于响应式调用,例如:

output$R1_C1 <- renderPlot({

beginning <- Sys.time()

<lots of code here> 

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

R 抱怨并给了我

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

有没有人在 Shiny 的响应式调用中找到了一种成功的方法来计时执行速度?

【问题讨论】:

    标签: r shiny execution-time


    【解决方案1】:

    同样,您可以使用流行的tictoc 包,如下所示。我更喜欢它的原因是您可以轻松地使用多个 tic-toc 标签来测量反应式或渲染表达式中的子例程。

    library(shiny)
    library(tictoc)
    
    runApp(list(
      ui = bootstrapPage(
        numericInput('n', 'Number of obs', 100),
        plotOutput('plot')
      ),
    server = function(input, output) {
        output$plot <- renderPlot({
        
        tic("execution time - Histgram")
        hist(runif(input$n)) 
        toc()
    
        tic("execution time - Print")
        print("this is a second task within 'renderPlot' ")
        toc()
      })
     }
    ))
    

    【讨论】:

      【解决方案2】:

      这适用于我的系统:

      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
          })
        }
      ))
      

      【讨论】:

        【解决方案3】:

        profvis 包可能很有用。示例:

        library(shiny)
        library(profvis)
        
        profvis({
        
          sApp <- shinyApp(
        
            ui = fluidPage(
              numericInput('n', 'Number of obs', 100, min = 1, max = 200),
              plotOutput('plot')
            ),
        
            server = function(input, output) {
        
              dfTable <- reactive({
                as.data.frame(matrix(rnorm(10 * input$n, mean = 5), ncol = input$n))
              })
        
              vMeans <- reactive({
                apply(dfTable(), 2, mean)
              })
        
              output$plot <- renderPlot({
                hist(vMeans())
              })
            }
          )
        
          runApp(sApp)
        })
        

        【讨论】:

          猜你喜欢
          • 2012-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-23
          • 1970-01-01
          • 2013-04-01
          • 1970-01-01
          相关资源
          最近更新 更多