在 rmarkdown pdf 输出中包装比例表的列名的有效方法 [英] Efficient way to wrap column names of proportion tables in rmarkdown pdf output

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

问题描述

我正在使用 questionr 包制作行比例的加权表.当列名太长时,我想换行.因为我正在制作数百个表,所以该解决方案需要处理具有不同列数的表.我还想避免将所有列设置为特定宽度.理想情况下,短列名称将保持其正常宽度,而超过指定最大长度的名称将被换行.

I'm making weighted tables of row proportions using the questionr package. I want to wrap the column names when they are too long. Because I'm making hundreds of tables, the solution needs to work on tables with varying numbers of columns. I also want to avoid setting all columns to a specific width. Ideally, short column names would remain at their normal width while names exceeding the specified maximum length would be wrapped.

以下是我迄今为止尝试过的一堆解决方案,编写为 .Rmd 文件:

Here are a bunch of solutions I've tried so far, written as .Rmd file:

---
title: "Example"
output: pdf_document
---

```{r setup, include=FALSE}
library(questionr)
library(knitr)
data("happy")
```


A simple weighted table with the "kable" method:
```{r table1, echo=TRUE}
kable(wtd.table(happy$degree, happy$happy, weights = happy$wtssall),
  digits = 0)
```

The same "kable" table, but with row proportions:
```{r table2, echo=TRUE}
kable(rprop(wtd.table(happy$degree, happy$happy, weights = happy$wtssall)),
  digits = 0)
```

I want to wrap the column headers, but kableExtra::column_spec() gives an error.
Even if it worked it requires manually setting each column width.:
```{r table3, echo=TRUE}
library(kableExtra)
kable(rprop(wtd.table(happy$degree, happy$happy, weights = happy$wtssall)),
  digits = 0) %>%
  column_spec(column = 2, width = ".25in")
```

Maybe str_wrap will do the trick?
```{r table4, echo=TRUE}
library(stringr)
kable(rprop(wtd.table(happy$degree, str_wrap(happy$happy, width = 8),
                  weights = happy$wtssall)),
  digits = 0)
```

Giving up on knitr::kable(), maybe pander has a solution.
Here is the simple weighted frequency table.
```{r table5, echo=TRUE, results='asis'}
library(pander)
pandoc.table(wtd.table(happy$degree, str_wrap(happy$happy, width = 8),
                  weights = happy$wtssall),
         split.cells=8)
```

So far, so good. But it doesn't work for the table of row proportions,
because the rprop table is of class ([1]"proptab" [2]"table")
while the wtd.table() is just class "table"
```{r table6, echo=TRUE, results='asis', error=TRUE}
pandoc.table(rprop(wtd.table(happy$degree, str_wrap(happy$happy, width = 8),
                  weights = happy$wtssall)),
         split.cells=8)
```

But wait! I can pass a kable() product as pandoc output.
This table looks great, but I don't think I pass any
pandoc.table() arguments like "split.cells=8" to it.
```{r table7, echo=TRUE, results='asis', error=TRUE}
kable(rprop(wtd.table(happy$degree, happy$happy, weights = happy$wtssall)),
  digits = 0, format = "pandoc")
```

.Rmd 文件的输出如下所示:

And here is what the output of that .Rmd file looks like:

推荐答案

至少,对于 kableExtra,您需要在 kable 函数中指定格式为 latexhtml.

At least, for kableExtra, you need to specify format in your kable function to be either latex or html.

要使其动态化,您可以在表进入 kable 之前将表保存到变量中,并在 column_spec 函数中使用 2:(ncol(your_table) + 1)(+1 表示 column_name列).

To make it dynamic, you can save the table to a variable before it goes into kable and use 2:(ncol(your_table) + 1) in the column_spec function (+1 for the column_name column).

这篇关于在 rmarkdown pdf 输出中包装比例表的列名的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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