如何将换行符放入 R 中 xtable 的列标题中
我有一个数据框,正在使用 xtable 将其放入 sweave 文档中,但是我的一个列名称很长,我想将其分成两行以节省空间
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)
我尝试在名称中添加换行符,但这似乎不起作用有没有
names(calqc_table)[1]<-"Identifier of the \nRun within the Study"
办法做到这一点?我看到有人建议使用 hmisc 包中的 Latex 函数手动迭代表格并手动用乳胶写出来,包括换行符,但这看起来有点像!
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"
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现执行此操作的最佳方法是将表格列指示为“固定宽度”列,以便其中的文本换行。使用
xtable
包,可以通过以下方式完成:xtable
要求您为选项“rownames”列提供对齐方式 - 这是初始的l规范。节规范
p{1.5in}
用于第一个列标题,该列标题相当长。这将其限制为宽度为 1.5 英寸的框,并且如果需要,标题将换行为多行。其余五列使用c
说明符设置居中。像
p{1.5in}
这样的固定宽度列的一个主要问题是它们使用合理的对齐方式设置文本。这会导致每行中的字间距扩大,使得该行将填满分配的整个 1.5 英寸。坦率地说,在大多数情况下,这会产生我无法用礼貌语言描述的结果(我是一个业余印刷爱好者,这种行为会导致面部蜱虫)。
修复方法是通过在列规范前添加
>{}
字段来提供 Latex 对齐命令:其他有用的对齐命令有:
请记住使用双反斜杠以在 R 字符串中转义它们。您可能还需要禁用 xtable 默认使用的字符串清理功能。
注意
如果在表格的最后一列上使用此对齐技术将会失败除非表格行以
\tabularnewline 而不是
\\
,我认为xtable
不是这种情况,并且不能通过任何用户可设置的选项轻松自定义。另一件需要考虑的事情是,您可能不希望整个列换行至 1.5 英寸并居中 - 只是标题。在这种情况下,请禁用
xtable
字符串清理并使用宽度为 1 的\multicolumn
单元格设置标题: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:xtable
demands that you provide an alignment for the option "rownames" column- this is the initiall
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 thec
specifier.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.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:Other useful alignment commands are:
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.Note
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 withxtable
and is not easily customizable through any user-settable option.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:@Sharpie 的技术对我不起作用,因为 pandoc 在转换为 pdf 时失败并出现错误 43。因此,我所做的就是:
移动
\\centering
标记:(此处应用于表格的所有 6 列)
并禁用 xtable 打印中的清理:
@Sharpie 's technique did not work for me, as pandoc failed with error 43 on conversion to pdf. Therefore, here is what I did:
moved the
\\centering
marker:(here applied to all 6 columns of the table)
and disabled sanitization in xtable printing:
以下解决方案对我有用:我首先将 xtable 保存在文件中,然后重新导入它(如malexan 所描述的此处),然后按照egreg 此处。棘手的事情是正确处理转义字符
"\"
。如果您想要分成两行的文本包含特殊正则表达式字符(有关这些字符的列表,请参见例如 此处),因为您需要在“模式”参数中匹配它们。下面您可以看到如何修改上述问题中的表格以将第一列名称分成两行。
生成的 LaTex 表格的屏幕截图
The following solution works for me: I first save the xtable in a file, then re-import it (as malexan describes here) and then replace the text which is to be browken into two lines as suggested by egreg here. The tricky thing is to geht the escape characters
"\"
right. This is also relevant if the text you want to break into two lines contains characters which are Special Regex Characters (for a list of these see e.g. here) as you need to match them in the "pattern" argument.Below you can see how the table from the question above can be modified to break the first column name in two lines.
Screenshot from the resulting LaTex table