I am falling in love with the htmlTable()
function in the Gmisc
package. My tables are so much prettier than they were before. In my table I have one column with quite large entries and I'm having a hard time keeping it wide enough that the numbers don't wrap. I would imagine either a nowrap
argument by column or a column.width
argument would work for this, but I can't seem to find either. Is there a way to do this? Or should I just settle for "pretty darn good"?

- 4,860
- 7
- 43
- 55
-
1That function doesn't appear to be documented in the specs: http://cran.r-project.org/web/packages/Gmisc/Gmisc.pdf – Roman Luštrik Aug 21 '14 at 07:35
-
Thank you @RomanLuštrik. I couldn't find it either, but I thought I might be missing something. – Tom Aug 22 '14 at 00:49
2 Answers
I know I am a little late to the party, but here are a few ways you could go about. And for the record, there are so many ways to tweak these tables to get them looking exactly the way you want. So IMO the options are to 1) have various arguments for every single cell coloring option, cell height and width, row height and width, column height and width, etc; or 2) let the user figure something out.
That being said here are some possible solutions:
library(Gmisc)
## solution 1: quick, dirty
tbl <- `colnames<-`(matrix(1:9, 3, 3), c('one','two','three'))
(tmp <- htmlTable(tbl))
tbl[1,1] <- 'this is a very long cell, this is a very long cell, this is a very long cell, this is a very long cell'
(tmp <- htmlTable(tbl))
tbl[1,1] <- gsub(' ', ' ', tbl[1,1])
htmlTable(tbl)
Basically just collapsing any whitespace and using nbsp instead
The next solution actually uses some legit html tags:
## solution 2:
tbl <- `colnames<-`(matrix(1:9, 3, 3), c('one','two','three'))
tbl[1,1] <- 'this is a very long cell, this is a very long cell, this is a very long cell, this is a very long cell'
(tmp <- htmlTable(tbl))
(tmp <- gsub('<td', '<td nowrap="nowrap"; ', tmp))
The solution above replaces all of the cell styles (td) with one that includes nowrap. Replacing all of the cells may or may not be what you want which led me to the next option: regex
## solution 3: regex
tbl <- `colnames<-`(matrix(1:9, 3, 3), c('one','two','three'))
tbl[1,1] <- 'this is a very long cell, this is a very long cell, this is a very long cell, this is a very long cell'
(tmp <- htmlTable(tbl))
regmatches(tmp, gregexpr('<td.*?</td>', tmp))
# [[1]]
# [1] "<td style='text-align: left;'>this is a very long cell, this is a very long cell, this is a very long cell, this is a very long cell</td>"
# [2] "<td style='text-align: center;'>4</td>"
# [3] "<td style='text-align: center;'>7</td>"
# [4] "<td style='text-align: left;'>2</td>"
# [5] "<td style='text-align: center;'>5</td>"
# [6] "<td style='text-align: center;'>8</td>"
# [7] "<td style='border-bottom: 1px solid grey; text-align: left;'>3</td>"
# [8] "<td style='border-bottom: 1px solid grey; text-align: center;'>6</td>"
# [9] "<td style='border-bottom: 1px solid grey; text-align: center;'>9</td>"
I didn't go on because I think one of the above would suit fine, and regex is not my strong suit (neither is html or css for that matter).
I'm sure there are other options similar to this one. For instance, you could try to insert a width tag into the column tags.

- 20,481
- 4
- 44
- 78
-
thanks for your insightful answer. This works for many situations I think. Your option 1 and 2 worked for me in different situations. Option 3 is maybe also out of my league! – Tom Nov 19 '14 at 03:31
To control column width
(and other row, column, or cell style elements)
Buried in the CRAN https://cran.r-project.org/web/packages/htmlTable/htmlTable.pdf , under "addHtmlTableStyle", "The css.cell argument": If css.cell is a vector, it’s assumed that the styles should be repeated throughout the rows (that is, each element in css.cell specifies the style for a whole column of ’x’).
So:
library("htmlTable")
library("tidyverse")
myDf <- data.frame(x=1:5, y=6:10)
myDf %>%
addHtmlTableStyle(css.cell = c("width: 50;","width: 100;")) %>%
htmlTable()
That "The css.cell argument" section in the htmlTable CRAN doc also explains how to control styles for each table cell.

- 444
- 1
- 4
- 15