简体   繁体   English

在r markdown中包装pandoc表列名

[英]Wrapping pandoc table column names in r markdown

I am creating a pandoc.table with long column name that I want to wrap so my table does not go off the pdf page. 我正在创建一个带有长列名的pandoc.table,我想要包装,所以我的表不会脱离pdf页面。 I know you can use split.tables, but that takes away the clarity of the table. 我知道你可以使用split.tables,但这会消除表格的清晰度。 Using split.cells doesn't seem to do anything, even when supplied as a vector. 即使作为向量提供,使用split.cells似乎也没有做任何事情。

---
  output : pdf_document
---

```{r,echo=FALSE, results="asis"}
library(pander)
mytab = data.frame(ReallySuperExtraLongColumnNameThatIWantToWrap=1:2, col2=2001:2002)
pandoc.table(mytab)
``` 

The following will produce a table with a line break in the header: 以下将生成一个在标题中包含换行符的表:

```{r,echo=FALSE, results="asis"}
library(pander)
mytab = data.frame("ReallySuperExtraLongColumn\nNameThatIWantToWrap"=1:2,
                  col2=2001:2002,
                  check.names = FALSE)
pandoc.table(mytab)
``` 

The line break is encoded with \\n . 换行符用\\n编码。 This is not an allowed character in a columnname, and the data.frame() function would normally remove it. 这不是列名中允许的字符, data.frame()函数通常会将其删除。 You can use check.names = FALSE to suppress this behaviour and keep the column names exactly as you entered them. 您可以使用check.names = FALSE来抑制此行为,并使列名完全按照您输入的名称保留。 Alternatively, you could redefine the column name on a separate line: 或者,您可以在单独的行上重新定义列名称:

mytab = data.frame(ReallySuperExtraLongColumnNameThatIWantToWrap=1:2, col2=2001:2002)
names(mytab)[1] = "ReallySuperExtraLongColumn\nNameThatIWantToWrap"

You can also set the width of the cells with split.cells . 您还可以使用split.cells设置单元格的宽度。 The line breaks will then be generated automatically, however, breaks only appear when there is a space in your column header. 然后会自动生成换行符,但只有在列标题中有空格时才会出现换行符。 An example: 一个例子:

```{r,echo=FALSE, results="asis"}
library(pander)
mytab = data.frame("Really Super Extra Long Column Name That I Want To Wrap"=1:2,
                  col2=2001:2002,
                  check.names = FALSE)
pandoc.table(mytab, split.cells = 15)
``` 

This gives breaks after "Extra" and "Name". 这会在“额外”和“名称”之后产生中断。 Note that you still need check.names = FALSE , because also spaces are not allowed in data frame names. 请注意,您仍需要check.names = FALSE ,因为数据框名称中也不允许使用空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM