用knitr将两个data.frame彼此对齐? [英] Align two data.frames next to each other with knitr?

查看:57
本文介绍了用knitr将两个data.frame彼此对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是knitr的新手(对R也很新),所以这可能是一个愚蠢的问题...

I'm new to knitr (and also quite new to R), so this might be a dumb question...

我有两个data.frame,它们都有两列,但行数不同.我想在编织报告中显示它们,但是当它们可以很容易地彼此相邻放置时,在另一个狭窄的桌子下面放一个狭窄的桌子看起来并不好. 有什么办法可以使它们彼此相邻显示?

I have two data.frames, which both have two columns, but different numbers of rows. I want to show them in my knitr report, but having one narrow table below another narrow table when they could so easily sit next to each other doesn't look good. Is there any way they could be displayed next to each other?

更新

好吧,根据以下建议,这就是我所做的事情(我现在将三张桌子放在一起):

Ok, based on the suggestion below, here's what I did (I'm putting three tables together now):

```{r fig.height=13.5, fig.width=10, echo=FALSE, comment=""}
grid.arrange(textGrob("Visual Clusters", gp=gpar(fontsize=14, fontface="bold")),
             textGrob("We have biofilm data for...", gp=gpar(fontsize=14, fontface="bold")),
             textGrob("Left Over Isolates", gp=gpar(fontsize=14, fontface="bold")),
             tableGrob(clusters, show.rownames=FALSE, gp=gpar(fontsize=10)),
             tableGrob(clust_ab, show.rownames=FALSE, gp=gpar(fontsize=10)),
             tableGrob(n_clust, show.rownames=FALSE, gp=gpar(fontsize=10)),
             ncol=3, nrow=2, heights=c(1,30))
```

这看起来已经非常不错了,三个表的标题都没有编号行.
到目前为止,我唯一无法解决的问题是,所有桌子都水平居中,因此,如果您知道我的意思,那么较短的桌子将从最长的桌子开始.

This looks already really good, with titles for the three tables and without the numbered rows.
The only issue I couldn't resolve so far is that the tables are all centred horizontally, so the shorter ones start below the longest one, if you know what I mean.

推荐答案

knitr的开发版本(在Github上为 ;请按照此处的安装说明进行操作)具有kable()函数,该函数可以将表作为字符向量返回.您可以收集两个表并将它们安排在父表的两个单元格中.这是一个简单的示例:

The development version of knitr (on Github; follow installation instructions there) has a kable() function, which can return the tables as character vectors. You can collect two tables and arrange them in the two cells of a parent table. Here is a simple example:

```{r two-tables, results='asis'}
library(knitr)
t1 = kable(mtcars, format='html', output = FALSE)
t2 = kable(iris, format='html', output = FALSE)
cat(c('<table><tr valign="top"><td>', t1, '</td><td>', t2, '</td><tr></table>'),
    sep = '')
```

您还可以使用CSS技巧(如style="float: [left|right]")将表格向左/向右浮动.

You can also use CSS tricks like style="float: [left|right]" to float the tables to the left/right.

如果要设置单元格的填充和间距,可以像往常一样使用表属性cellpadding/cellspacing,例如

If you want to set cell padding and spacing, you can use the table attributes cellpadding / cellspacing as usual, e.g.

```{r two-tables, results='asis'}
library(knitr)
t1 = kable(mtcars, format='html', table.attr='cellpadding="3"', output = FALSE)
t2 = kable(iris, format='html', table.attr='cellpadding="3"', output = FALSE)
cat(c('<table><tr valign="top"><td>', t1, '</td>', '<td>', t2, '</td></tr></table>'),
    sep = '')
```

有关上述代码的实际操作,请参见 RPubs帖子.

See the RPubs post for the above code in action.

这篇关于用knitr将两个data.frame彼此对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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