Documentos dinâmicos

Sweave

Prof. Walmes Zeviani

2018-03-22

Justificativa:

Objetivos

Pesquisa reproduzível

Literate Programming (LP)

Reproducible Research (RR)

Vamos desenhar isso!

Sweave

Descrição

Processo

O processo é baseado em duas etapas.

No R, os comandos a seguir executam as etapas mencionadas.

  1. Sweave("arquivo.Rnw"): interpreta os fragmentos de código R e cria o .tex.
  2. tools::texi2pdf("arquivo.tex"): compila o .tex para .pdf.
  3. Stangle("arquivo.Rnw"): extraí o conteúdo dos fragmentos de código R e cria o .R.

Prós e contras

Knitr

Sobre

knitr =
    Sweave +
    cacheSweave +
    pgfSweave +
    weaver +
    animation::saveLatex +
    R2HTML::RweaveHTML +
    highlight::HighlightWeaveLatex +
    0.2 * brew +
    0.1 * SweaveListingUtils +
    more

Vantagens

Mais opções para controle

Exportar tabelas

Com o knitr pode-se produzir documentos finais em PDF usando LaTeX ou Markdown (R markdown) e documento HTML usando HTML (Rhtml) ou Markdown.

Para exportar tabelas para renderização em markdown usa-se a função knitr::kable().

library(knitr)
kable(mtcars[1:2, 1:3])
|              | mpg| cyl| disp|
|:-------------|---:|---:|----:|
|Mazda RX4     |  21|   6|  160|
|Mazda RX4 Wag |  21|   6|  160|

Para exportar para LaTeX pode-se usar a xtable::xtable(). Com a opção type = "html" exporta-se para HTML.

library(xtable)
xtable(mtcars[1:2, 1:3])
% latex table generated in R 3.4.4 by xtable 1.8-2 package
% Thu Mar 22 20:13:05 2018
\begin{table}[ht]
\centering
\begin{tabular}{rrrr}
  \hline
 & mpg & cyl & disp \\ 
  \hline
Mazda RX4 & 21.00 & 6.00 & 160.00 \\ 
  Mazda RX4 Wag & 21.00 & 6.00 & 160.00 \\ 
   \hline
\end{tabular}
\end{table}
library(xtable)
print(xtable(mtcars[1:2, 1:3]), type = "html")
<!-- html table generated in R 3.4.4 by xtable 1.8-2 package -->
<!-- Thu Mar 22 20:13:05 2018 -->
<table border=1>
<tr> <th>  </th> <th> mpg </th> <th> cyl </th> <th> disp </th>  </tr>
  <tr> <td align="right"> Mazda RX4 </td> <td align="right"> 21.00 </td> <td align="right"> 6.00 </td> <td align="right"> 160.00 </td> </tr>
  <tr> <td align="right"> Mazda RX4 Wag </td> <td align="right"> 21.00 </td> <td align="right"> 6.00 </td> <td align="right"> 160.00 </td> </tr>
   </table>

Outros pacotes para tabela

A task view de Reproducible Research concentra os principais recursos disponíveis para geração automática de documentos. O pacote tables, Hmisc e reporttools são alguns dos pacotes com funcionalidades para LaTeX.

library(tables)

tb <- tabular((Species + 1) ~
                  (n = 1) +
                  Format(digits = 2) *
                  (Sepal.Length + Sepal.Width) *
                  (mean + sd),
              data = iris)
tb
##                                                   
##                 Sepal.Length      Sepal.Width     
##  Species    n   mean         sd   mean        sd  
##  setosa      50 5.01         0.35 3.43        0.38
##  versicolor  50 5.94         0.52 2.77        0.31
##  virginica   50 6.59         0.64 2.97        0.32
##  All        150 5.84         0.83 3.06        0.44
Hmisc::latex(tb)
\begin{tabular}{lccccc}
\hline
 &  & \multicolumn{2}{c}{Sepal.Length} & \multicolumn{2}{c}{Sepal.Width} \\ 
Species  & n & mean & sd & mean & \multicolumn{1}{c}{sd} \\ 
\hline
setosa  & $\phantom{0}50$ & $5.01$ & $0.35$ & $3.43$ & $0.38$ \\
versicolor  & $\phantom{0}50$ & $5.94$ & $0.52$ & $2.77$ & $0.31$ \\
virginica  & $\phantom{0}50$ & $6.59$ & $0.64$ & $2.97$ & $0.32$ \\
All  & $150$ & $5.84$ & $0.83$ & $3.06$ & $0.44$ \\
\hline 
\end{tabular}

Subir o código dessa tabela no https://www.tablesgenerator.com/ para ver como ficou.

Próxima aula