钩到时间编织块 [英] Hook to time knitr chunks

查看:12
本文介绍了钩到时间编织块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.
", 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:

egin{kframe}

% Chunk rendering time: 12.786 seconds.

end{kframe}

Is there a way to produce unwrapped comments?

解决方案

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}
', x, '
\begin{kframe}')
    }
  })
})

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

这篇关于钩到时间编织块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆