在Rmarkdown中并排Xtables [英] Side by side Xtables in Rmarkdown

查看:61
本文介绍了在Rmarkdown中并排Xtables的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了在RMarkdown-HTML中创建并排xtable的答案 knitr,R Markdown和xtable:HTML中的xtable表桌子

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

以及如何直接在Sweave中创建并排xtable R:使用xtable()打印两个表

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

但是在Rmarkdown/Pandoc中并排xtables呢?

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

我的* Rmd文件中有

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

这些显示为单独的表格-很好!但是如何使它们显示为并排子图/子表?我试图将围绕{r}print(xtable)代码的Latex子图代码进行集成,但无济于事.

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.

推荐答案

好吧,使用R markdown生成它很容易.下面是我的代码和结果:

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

我结合了您链接到的示例:

I combined the example you linked to:

这是.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文件位于同一文件夹中:

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

\usepackage{subfig}
\usepackage{graphicx}

这篇关于在Rmarkdown中并排Xtables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆