5

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

sports

share of ballers    22.3 
share of skiers      4.6

addiction

share of smokers    20.3 
share of drinkers    6.6

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:

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

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?

Community
  • 1
  • 1
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207

1 Answers1

12

For the hline part, see ?print.xtable.

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)

e.g:

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)
Henrik
  • 65,555
  • 14
  • 143
  • 159
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • +1 for the pointer. lost track a little bit here after all this docu. However hline seems to be limited to only hline. Something simlar that does a little more like spacing or inser characters would be perfect. – Matt Bannert Dec 07 '12 at 12:14
  • where exactly do you want insert character? – agstudy Dec 07 '12 at 12:15
  • Row names for example. Like in my example. Adding the row names of the bold section headers would be cool. I would not have to add them to my R table then. – Matt Bannert Dec 07 '12 at 12:24
  • thx for the effort. But still the problem is, that I just want to boldify some of the rows like in the example. – Matt Bannert Dec 09 '12 at 10:18
  • At the end of the day I guess you are right,gotta use gsub somewhere here, tried to avoid it :). – Matt Bannert Dec 09 '12 at 13:56