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"?
Can I control word wrap or column width in htmlTable?
3.9k Views Asked by Tom At
2
There are 2 best solutions below
0
On
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.
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:
Basically just collapsing any whitespace and using nbsp instead
The next solution actually uses some legit html tags:
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
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.