2015-05-29 9 views
5

Desidero misurare i blocchi di knitr e registrare il tempo necessario per renderli utilizzando i commenti nell'output di LaTeX.Hook to time knitr chunks

Ho provato il seguente gancio:

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

e lo fa produrre il commento corretto con temporizzazione ma il problema è che è avvolto in kframe che si traduce in brutte lacune nell'output LaTeX:

\begin{kframe} 

% Chunk rendering time: 12.786 seconds. 

\end{kframe} 

C'è un modo per produrre commenti non organizzati?

+0

FWIW, è stato crossato su https://github.com/yihui/knitr/issues/1042 –

+0

Avete un MWE che indica _ugly gaps_? – Thell

risposta

3

Prova questo:

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

Si tratta di un hack, però. Fondamentalmente si evita il commento LaTeX dall'ambiente kframe.