rlatexoutputpdflatex

How to use the 'tables' package to write data.frame to PDF table?


I have a data frame. For example, it looks like this:

Condition    dv1_mean    dv2_mean
A               1           2
A               2           3
A               2           3
B               3           4
B               3           4
B               3           4
C               3           4
C               3           4
C               3           4

My code is below:

stderr <- function(x) sd(x)/sqrt(length(x))

table<-toLatex(
  tabular( (Condition + 1) ~ (n=1) + Format(digits=2)*
            (dv1_mean + dv2_mean)*(mean + sd+median+stderr), data=accuracy ))

table

The output of my code is a bunch of unformatted messy codes like below when I click on the knit PDF:

\begin{tabular}{lccccccccc}
\hline
 &  & \multicolumn{4}{c}{dv1_mean} & \multicolumn{4}{c}{dv2_mean} \\ 
Condition  & n & mean & sd & median & stderr & mean & sd & median & \multicolumn{1}{c}{stderr} \\ 
\hline
C1: Control  & $\phantom{0}433$ & $\phantom{-}0.015$ & $\phantom{-}0.487$ & $-0.172$ & $\phantom{-}0.023$ & $\phantom{-}0.022$ & $\phantom{-}0.595$ & $-0.199$ & $\phantom{-}0.029$ \\
C2: Brier-Continuous  & $\phantom{0}423$ & $-0.063$ & $\phantom{-}0.452$ & $-0.243$ & $\phantom{-}0.022$ & $-0.142$ & $\phantom{-}0.532$ & $-0.339$ & $\phantom{-}0.026$ \\
C3: Superforecaster  & $\phantom{00}17$ & $-0.306$ & $\phantom{-}0.129$ & $-0.309$ & $\phantom{-}0.031$ & $-0.266$ & $\phantom{-}0.346$ & $-0.354$ & $\phantom{-}0.084$ \\
C4: Reciprocal-Continuous  & $\phantom{0}428$ & $-0.031$ & $\phantom{-}0.484$ & $-0.234$ & $\phantom{-}0.023$ & $-0.062$ & $\phantom{-}0.582$ & $-0.287$ & $\phantom{-}0.028$ \\
All  & $1301$ & $-0.030$ & $\phantom{-}0.474$ & $-0.215$ & $\phantom{-}0.013$ & $-0.063$ & $\phantom{-}0.572$ & $-0.280$ & $\phantom{-}0.016$ \\
\hline 
\end{tabular}

How could I use the tables package and the toLatex function correctly?


Solution

  • You need to tell knitr to leave the output alone when processing it. To do this, put results="asis" in the chunk that produces the table. For example, in R Markdown:

    ---
    title: "Untitled"
    output: pdf_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    `## R Markdown
    
    This is an R Markdown document.
    
    ```{r results='asis'}
    accuracy <- data.frame(Condition = factor(rep(c("A", "B", "C"), each = 3)),
                           dv1mean = c(1,2,2,3,3,3,3,3,3),
                           dv2mean = c(2,3,3,4,4,4,4,4,4))
    
    stderr <- function(x) sd(x)/sqrt(length(x))
    
    library(tables)
    
    table<-toLatex(
      tabular( (Condition + 1) ~ (n=1) + Format(digits=2)*
                (dv1mean + dv2mean)*(mean + sd+median+stderr), data=accuracy ))
    
    table
    ```
    

    enter image description here

    Note that I made a couple of other changes: Condition needs to be a factor, and the underscores in the names dv1_mean and dv2_mean cause issues in Latex, so I removed them. You could alternatively set explicit column headings using Heading. For example, Heading("$dv_1$-mean")*dv1_mean instead of just dv1_mean would produce

    enter image description here