使用 print.xtables 添加行或加粗单个 row.names - 在行之间添加一些东西? [英] adding rows or boldify single row.names with print.xtables – add something in between rows?

查看:13
本文介绍了使用 print.xtables 添加行或加粗单个 row.names - 在行之间添加一些东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 print.xtable add.to.row 来获得如下格式的表格:

I try to use print.xtable add.to.row to get table formatted like this:

运动

share of ballers    22.3 
share of skiers      4.6

上瘾

share of smokers    20.3 
share of drinkers    6.6

我的 R 表确实包含额外的 row.names,即使这些行不包含任何值.我使用 add.to.row 选项将颜色添加到不同的行,如建议的 here 效果很好.但不起作用的是使用 xtable 添加粗体文本或在行之间添加额外的 hline .我总是收到如下错误消息:

My R table does contain the additional row.names even if these rows don't contain any values. I used the add.to.row option to add colors to the different rows like suggested here which worked fine. But what does not work is to add bold text with xtable or add additional hline between the rows. I always get an error message like:

类型区​​域设置错误! 检测到的线宽大约比启发式确定的线宽大 52 % (typearea).

Bad type area settings! The detected line width is about 52 % (typearea) larger than the heuristically determined line width.

因此,这可能意味着由于我的更改,表在其环境中变大了,但不知道该怎么做.注意我读过这样的帖子 一个 修改 xtable 输出本身,但即使这对我来说可能是可能的,但我正在寻找一个更简单的解决方案.因为如果我选择这个解决方案,我必须 capture.output 并使用正则表达式替换在两者之间添加一些东西.

So this could mean the table gets to big for its environment because of my changes, but do not have an idea what to do about it. Note I have reads posts like this one that modifies the xtable output itself, but even though this might possible for me to I am looking for an easier solution. Because if I go for this solution I had to capture.output and use regexp replacement to add something in between.

有没有办法解决这个问题?还是有更简单的解决方案?

Is there a way around this? Or is there a simpler solution?

推荐答案

hline部分见?print.xtable.

hline.after:当 'type="latex"' 时,一个介于 -1 和'"nrow(x)"',包括应该出现水平线

hline.after: When 'type="latex"', a vector of numbers between -1 and '"nrow(x)"', inclusive, indicating the rows after which a horizontal line should appear

为了让你所有的行名更加大胆:

To embolden all you rows names:

bold.allrows <- function(x) {
  h <- paste('\textbf{',x,'}', sep ='')
  h
}
print(xtable(yourTable), 
      sanitize.rownames.function =  bold.allrows)

要使某些行名称更加大胆,您可以为这些行添加特殊标记",例如粗体:

To embolden some row names, you can add a " special markup" to those rows, e.g. BOLD:

bold.somerows <- 
        function(x) gsub('BOLD(.*)',paste('\\textbf{\1','}'),x)

print(xtable(yourTable), 
      sanitize.rownames.function =  bold.somerows)

例如:

require(xtable)
hh <- head(mtcars)[ , c(1:5)]
## I want to bold 1 and 3 rows 
rownames(hh)[c(1, 3)] <- paste('BOLD', rownames(hh)[c(1, 3)])
print(xtable(hh), sanitize.rownames.function =  bold.somerows)

这篇关于使用 print.xtables 添加行或加粗单个 row.names - 在行之间添加一些东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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