如何在R中的xtable中将换行符放入列标题中 [英] How to put a newline into a column header in an xtable in R

查看:85
本文介绍了如何在R中的xtable中将换行符放入列标题中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要放入 sweave 文档中的数据框使用xtable,但是我的列名之一很长,我想将其分成两行以节省空间

I have a dataframe that I am putting into a sweave document using xtable, however one of my column names is quite long, and I would like to break it over two lines to save space

calqc_table<-structure(list(RUNID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), ANALYTEINDEX = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), ID = structure(1:11, .Label = c("Cal A", "Cal B", "Cal C", 
"Cal D", "Cal E", "Cal F", "Cal G", "Cal H", "Cal High", "Cal Low", 
"Cal Mid"), class = "factor"), mean_conc = c(200.619459644855, 
158.264703128903, 102.469121407733, 50.3551544728544, 9.88296440865076, 
4.41727762501703, 2.53494715706024, 1.00602831741361, 199.065054555735, 
2.48063347296935, 50.1499780776199), sd_conc = c(2.3275711264554, 
NA, NA, NA, NA, NA, NA, 0.101636943231162, 0, 0, 0), nrow = c(3, 
1, 1, 1, 1, 1, 1, 3, 2, 2, 2)), .Names = c("Identifier of the Run within the Study", "ANALYTEINDEX", 
"ID", "mean_conc", "sd_conc", "nrow"), row.names = c(NA, -11L
), class = "data.frame")
calqc_xtable<-xtable(calqc_table)

我尝试在名称中添加换行符,但这似乎不起作用

I have tried putting a newline into the name, but this didn't seem to work

names(calqc_table)[1]<-"Identifier of the \nRun within the Study"

有没有办法做到这一点?我看到有人建议使用 hmisc 包中的乳胶功能手动遍历表并手动将其写在乳胶中,包括换行符,但这似乎有点让人讨厌!

Is there a way to do this ? I have seen someone suggest using the latex function from the hmisc package to manually iterate over the table and write it out in latex manually, including the newline, but this seems like a bit of a faf !

推荐答案

我发现做到这一点的最佳方法是将表列指示为固定宽度"列,以使其中的文字换行.使用 xtable 包,可以通过以下方式完成:

The best way I have found to do this is to indicate the table column as a "fixed width" column so that the text inside it wraps. With the xtable package, this can be done with:

align( calqc_xtable ) <- c( 'l', 'p{1.5in}', rep('c',5) )

xtable 要求您为选项"rownames"列提供对齐方式-这是初始的 l 规范.部分规范 p {1.5in} 用于您的第一列标题,该标题很长.这将其限制为宽度为1.5英寸的盒子,并且如果需要,标题可以缠绕到多行上.其余五列使用 c 说明符居中设置.

xtable demands that you provide an alignment for the option "rownames" column- this is the initial l specification. The section specification, p{1.5in}, is used for your first column header, which is quite long. This limits it to a box 1.5 inches in width and the header will wrap onto multiple lines if necessary. The remaining five columns are set centered using the c specifier.

固定宽度列(例如 p {1.5in} )的一个主要问题是它们使用合理的对齐方式设置文本.这会导致每行中的单词间间距扩大,从而使该行将填满分配的整个1.5英寸.

One major problem with fixed width columns like p{1.5in} is that they set the text using a justified alignment. This causes the inter-word spacing in each line to be expanded such that the line will fill up the entire 1.5 inches allotted.

坦率地说,在大多数情况下,这会产生无法用礼貌的语言描述的结果(我是一个业余的排版螺母,这种行为会引起面部tick痒).

Frankly, in most cases this produces results which I cannot describe using polite language (I'm an amateur typography nut and this sort of behavior causes facial ticks).

解决方法是通过在列规范之前添加> {} 字段来提供乳胶对齐命令:

The fix is to provide a latex alignment command by prepending a >{} field to the column specification:

align( calqc_xtable ) <- c( 'l', '>{\\centering}p{1.5in}', rep('c',4) )

其他有用的对齐命令是:

Other useful alignment commands are:

  • \ raggedright->使文本左对齐
  • \ raggedleft->导致文本右对齐

请记住要使用双反斜杠将其转义为R字符串.您可能还需要禁用 xtable 默认使用的字符串清理功能.

Remember to double backslashes to escape them in R strings. You may also need to disable the string sanitation function that xtable uses by default.

注意

如果在表的最后一列使用此对齐技术,则将失败,除非表行以 \ tabularnewline 而不是结束> \\ ,我认为 xtable 并非如此,并且不容易通过任何用户可设置的选项进行自定义.

This alignment technique will fail if used on the last column of a table unless table rows are ended with \tabularnewline instead of \\, which I think is not the case with xtable and is not easily customizable through any user-settable option.

要考虑的另一件事是,您可能不希望将整个列换行为1.5英寸并居中对齐,而只是将标题对齐.在这种情况下,请禁用 xtable 字符串清理功能,并使用宽度为1的 \ multicolumn 单元格设置标题:

The other thing to consider is that you may not want the entire column line-wrapped to 1.5 inches and centered- just the header. In that case, disable xtable string sanitization and set your header using a \multicolumn cell of width 1:

names(calqc_table)[1]<-"\\multicolumn{1}{>{\\centering}p{1.5in}}{Identifier of the Run within the Study}"

这篇关于如何在R中的xtable中将换行符放入列标题中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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