Hook to time knitr chunks

天涯浪子 提交于 2019-12-30 18:05:11

问题


I would like to time knitr chunks and record how long it took to render them using comments in LaTeX output.

I've tried the following hook:

 now = Sys.time()
 knit_hooks$set(timeit = function(before) {
     if (before) { now <<- Sys.time() }
     else {
         paste("%", sprintf("Chunk rendering time: %s seconds.\n", round(Sys.time() - now, digits = 3))) 
     }
 })

And it does produce the correct comment with timing but the problem is that it's wrapped in kframe which results in ugly gaps in the LaTeX output:

\begin{kframe}

% Chunk rendering time: 12.786 seconds.

\end{kframe}

Is there a way to produce unwrapped comments?


回答1:


Try this:

local({
  now = Sys.time()
  knit_hooks$set(timeit = function(before) {
    if (before) {
      now <<- Sys.time()
    } else {
      x = round(Sys.time() - now, digits = 3)
      x = sprintf("%% Chunk rendering time: %s seconds.", x)
      paste('\\end{kframe}\n', x, '\n\\begin{kframe}')
    }
  })
})

It is a hack, though. Basically you escape the LaTeX comment from the kframe environment.



来源:https://stackoverflow.com/questions/30530008/hook-to-time-knitr-chunks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!