在 rmarkdown 中包装宽表 [英] wrapping wide table in rmarkdown

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

问题描述

我有一个非常宽的表格(超过300列),并且想通过包装这些列来显示它.在示例中,我将仅使用100列.

我要记住的是重复使用kable来显示表的子集:

 库(kableExtra)set.seed(1)数据= data.frame(矩阵(rnorm(300,10,1),ncol = 100))kable(data [,1:5],'latex',booktabs = T)kable(data [,6:10],'latex',booktabs = T)kable(data [,11:15],'latex',booktabs = T) 

但这显然很乏味...我知道有按比例缩小的选项,但是由于我有那么多的列,所以这是不可能的.

有没有我可以在 kable 中扭曲的参数来实现它?


已更新:@ jay.sf的答案似乎运行良好,但在此处未产生相同的结果.取而代之的是我得到了一些简单的代码-请您再看一眼,让我知道我可以改进什么地方?谢谢!

我的 sessionInfo()是:具有 rmarkdown :: pandoc_version() R版本3.5.1(2018-07-02) 1.19.2.1 .

解决方案

这个问题实际上比我第一眼想象的要棘手.我使用了一些 tidyverse 函数,特别是 dplyr :: select 来获取列,而使用 purrr :: map 来沿列索引组移动.

我的想法是制作一个列索引向量的列表以供选择,这样第一个列表项是 1:20 ,第二个列表项是 21:40 ,依此类推,以便将数据分成20个表,每个表5列(您使用的数字可以是 ncol(data)的不同因子).我低估了这样做的工作量,但是从

I have a really wide table (300+ columns) and would like to display it by wrapping the columns. In the example I will just use 100 columns.

What I have in mind is repetitively using kable to display the subset of the table:

library(kableExtra)
set.seed(1)
data = data.frame(matrix(rnorm(300, 10, 1), ncol = 100))

kable(data[, 1:5], 'latex', booktabs = T) 
kable(data[, 6:10], 'latex', booktabs = T) 
kable(data[, 11:15], 'latex', booktabs = T) 

But this is apparently tedious... I know there are scaling down options but since I have so many columns, it won't be possible.

Is there any parameter I can twist in kable to make it happen?


Updated: @jay.sf 's answer seems working well, but it didn't yield the same result here. Instead I got some plain code - could you please have a second look and let me know where can I improve? Thanks!

my sessionInfo() is: R version 3.5.1 (2018-07-02) with rmarkdown::pandoc_version() of 1.19.2.1.

解决方案

This question is actually trickier than I thought at first glance. I used some tidyverse functions, specifically dplyr::select to get columns and purrr::map to move along groups of column indices.

My thinking with this was to make a list of vectors of column indices to choose, such that the first list item is 1:20, the second is 21:40, and so on, in order to break the data into 20 tables of 5 columns each (the number you use can be a different factor of ncol(data)). I underestimated the work to do that, but got ideas from an old SO post to rep the numbers 1 to 20 along the number of columns, sort it, and use that as the grouping then to split the columns.

Then each of those vectors becomes the column indices in select. The resulting list of data frames each gets passed to knitr::kable and kableExtra::kable_styling. Leaving things off there would get map's default of printing names as well, which isn't ideal, so I added a call to purrr::walk to print them neatly.

Note also that making the kable'd tables this way meant putting results="asis" in the chunk options.

---
title: "knitr chunked"
output: pdf_document
---

```{r include=FALSE}
library(knitr)
library(kableExtra)
library(dplyr)
library(purrr)

set.seed(1)
data = data.frame(matrix(rnorm(300, 10, 1), ncol = 100))
```

```{r results='asis'}
split(1:ncol(data), sort(rep_len(1:20, ncol(data)))) %>%
  map(~select(data, .)) %>%
  map(kable, booktabs = T) %>%
  map(kable_styling) %>%
  walk(print)
```

Top of the PDF output:

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

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