在Rmarkdown并排Xtables

时间:2014-05-29 05:58:47

标签: r latex markdown knitr

我已经看到了在RMarkdown-HTML中创建并排xtables的答案 knitr, R Markdown, and xtable: xtable tables within HTML table

以及如何在Sweave中直接创建并排xtables R: Print two tables with xtable ()

但是在Rmarkdown / Pandoc中如何并排xtables?

在我的* Rmd文件中我有

```{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')
```

然后我编译如下:

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

这些显示为单独的表 - 好!但是如何让它们作为并排的子图/子表显示?我试图在{r}print(xtable)代码周围集成Latex子图代码无济于事。

1 个答案:

答案 0 :(得分:16)

好的,使用R markdown很容易制作它。以下是我的代码和结果:

我将您链接到的示例结合起来:

这是.Rmd文件的代码:

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

以下是header2.tex文件的代码,需要与.Rmd文件位于同一文件夹中:

\usepackage{subfig}
\usepackage{graphicx}

Result