Side by side Xtables in Rmarkdown
Asked Answered
J

1

14

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.

Jylland answered 29/5, 2014 at 5:58 Comment(2)
Could not find object "lach" dude :PUlcerative
Sorry, I know I didn't provide a working example.Jylland
U
17

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

Ulcerative answered 20/8, 2014 at 20:53 Comment(8)
Wow interesting. I learned more than a few things from your response. I don't know yet if it'll actually work (especially since I think I've converted to Rstudio instead of *rmd +pandoc) --but-- very helpful post regardless. Thank you for your detailed response.Jylland
Thanks - this was very useful! I do want to point out you have cache = TRUE twice in the options line. I had removed one, and I couldn't figure out why things weren't updating! :DPyrexia
This doesn't work in HTML. Any suggestion how to achieve the same result in HTML?Rayon
Yes because xtables are for latex/pdf output :) If you creat a separate question with that and post here a link to it, then I'll try to help you out.Ulcerative
xtable is for html output too, but yes, totally separate answer.Linnet
@MarcinKosiński I'm playing around with caption placement with this method, but have not had any success adding caption.placement = to the print call. Any thoughts? Not a latex expert.Linnet
@Clayton can you open additional issue for that? and leave here a link to it?Ulcerative
This is great. But how do you align those two tables vertically (top-align) - when they are of unequal row numbers. I am very happy to open a new question for that.Appoint

© 2022 - 2024 — McMap. All rights reserved.