14
votes

I have seen answers to creating side by side xtables in RMarkdown-HTML knitr, R Markdown, and xtable: xtable tables within HTML table

and how to create side by side xtables in Sweave directly R: Print two tables with xtable ()

But how about side by side xtables in Rmarkdown/Pandoc?

In my *Rmd file I have

```{r , results='asis', message=FALSE, echo=FALSE}
female.bmi <- lm(bmi ~ AGEGROUP + RACE + GEO_SPA + FPL_FIN + as.factor(year),
              data=lach[lach$GENDER=='Female',] )
xtable(female.bmi, comment=FALSE, caption = 'Linear regression of BMI for females')
male.bmi <- lm(bmi ~ AGEGROUP + RACE + GEO_SPA + FPL_FIN + as.factor(year),
            data=lach[lach$GENDER=='Male',] )
xtable(male.bmi, comment=FALSE, caption = 'Linear regression of BMI for males')
```

then I compile as follows:

knit('Modeling/simple.rmd', 'Modeling/simple.md') # creates md file
pandoc('Modeling/simple.md', format='latex') # LaTeX/PDF

These show up as separate tables - good! But how can I get them to display as side by side subfigures/subtables? I've tried to integrate Latex subfigure code around the {r}print(xtable) code to no avail.

1
Could not find object "lach" dude :PMarcin Kosiński
Sorry, I know I didn't provide a working example.Robin Donatello

1 Answers

17
votes

Ok it is very easy to produce it using R markdown. Below is my code and result:

I combined the example you linked to:

This is the code of .Rmd file:

---
title: " 2 tables in markdown side by side"
author: "Marcin Kosiński"
date: "2014"
output: 
   pdf_document:
      includes:
         in_header: header2.tex
      highlight: pygments
      toc: true
      toc_depth: 3
      number_sections: true
---

```{r,echo=FALSE}
library(knitr)
opts_chunk$set(comment="", message=FALSE,tidy.opts=list(keep.blank.line=TRUE, width.cutoff=120),options(width=100), cache=TRUE,fig.align='center',fig.height=6, fig.width=10,fig.path='figure/beamer-',fig.show='hold',size='footnotesize', cache=TRUE)
```

```{r}
library(xtable)
data(tli)
attach(tli)
x <- tli
fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=x)
print(xtable(fm1), file="ta.tex", floating=FALSE)
print(xtable(head(tli, n=5)), file="tb.tex", floating=FALSE)
```

\begin{table}[ht]
\centering
\subfloat[Table x(a)]{\label{tab:tab1a}\scalebox{.5}{\input{./ta}}}\quad
\subfloat[Table x(b)]{\label{tab:tab1b}\scalebox{.5}{\input{./tb}}}
\caption{Caption about here}
\label{tab:tab1}
\end{table}

And here is a code of header2.tex file that need to be in the same folder as .Rmd file:

\usepackage{subfig}
\usepackage{graphicx}

Result