Can I control word wrap or column width in htmlTable?

I fall in love with the function htmlTable()in the package Gmisc. My tables are much prettier than before. In my table, I have one column with fairly large entries, and it is difficult for me to hold it wide enough so that the numbers do not wrap around. I would suggest that a nowrapcolumn argument or an argument would be used for this column.width, but I cannot find either. Is there any way to do this? Or should I just agree to "pretty darn good"?

+4
source share
1 answer

I know I'm a little late to the party, but here are a few ways you could go. And for the record, there are so many ways to customize these tables so that they look exactly the way you want. Thus, IMO has the following parameters: 1) have different arguments for each cell coloring option, cell height and width, column height and width, column height and width, etc .; or 2) let the user understand something.

Here are some of the possible solutions:

library(Gmisc)

## solution 1: quick, dirty
tbl <- `colnames<-`(matrix(1:9, 3, 3), c('one','two','three'))
(tmp <- htmlTable(tbl))

enter image description here

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))

enter image description here

tbl[1,1] <- gsub(' ', '&nbsp;', tbl[1,1])
htmlTable(tbl)

enter image description here

Basically just folding any spaces and using nbsp instead

The following solution actually uses some legitimate 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))

enter image description here

The solution above replaces all cell styles (td) with one that includes nowrap. Replacing all cells may or may not be what you want, which led me to the following 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>" 

, , , - ( html css).

, , . , .

+4

All Articles