gforge / htmlTable

An R package for generating advanced tables
79 stars 24 forks source link

colspan and rowspan #37

Open ChengQian123 opened 7 years ago

ChengQian123 commented 7 years ago

Hi Max,

htmlTable() creates three-line tables as its default results. But i want to draw all td borders in my tables. My table using this function is below:

  2015  2016  2017
TypeName   mean sd cv   mean sd cv   mean sd cv
Type1   98.73 11.02 0.11   101.22 10.41 0.10   95.22 13.00 0.14
Type2   98.53 26.28 0.27   93.11 27.47 0.30   90.45 26.85 0.30
Type3   98.16 26.51 0.27   98.25 27.61 0.28   96.37 28.13 0.29
Type4   66.63 14.94 0.22   64.30 12.13 0.19   61.86 11.91 0.19
Type5   74.66 12.38 0.17   49.27 13.57 0.28   58.46 13.40 0.23
Type6   54.64 12.64 0.23   52.98 12.67 0.24   44.16 11.84 0.27
Type7   487.38 98.28 0.20   458.54 89.22 0.19   446.51 86.89 0.19

Blank columns among cgroups are not what i want.
So,two questions:

  1. How can i do rowspan in htmlTable(),e.g. "TypeName" in my table.
  2. How can i drop the Blank columns among cgroups.

And this is what i want:

TypeName 2015 2016 2017
mean sd cv mean sd cv mean sd cv
Type1 98.73 11.02 0.11 101.22 10.41 0.10 95.22 13.00 0.14
Type2 98.53 26.28 0.27 93.11 27.47 0.30 90.45 26.85 0.30
Type3 98.16 26.51 0.27 98.25 27.61 0.28 96.37 28.13 0.29
Type4 66.63 14.94 0.22 64.30 12.13 0.19 61.86 11.91 0.19
Type5 74.66 12.38 0.17 49.27 13.57 0.28 58.46 13.40 0.23
Type6 54.64 12.64 0.23 52.98 12.67 0.24 44.16 11.84 0.27
Type7 487.38 98.28 0.20 458.54 89.22 0.19 446.51 86.89 0.19

So how can i do that using htmlTable()?

Thanks so much! Qian

gforge commented 7 years ago

The empty column is a odd design choice that I've made on purpose. I like underlining the headers and this was the only way that I got it to work. The table you suggest is actually rather trivial to generate and perhaps some of the simpler table packages have a better solution. You could try to do str_replace on "<td>&nbsp;</td>" but it's only a hack.

ChengQian123 commented 7 years ago

Thanks for your solutions. I think you can consider embed the option dropping empty column in this function. :)

gforge commented 7 years ago

I have nothing against adding this functionality but I currently lack the time for implementing this. Help is always welcome and it shouldn't be that tricky if you want to give it a try. Basically:

  1. add an argument, e.g. skip_separator_cols (I think drop_empty_cols will be confusing as most don't reflect over these columns.
  2. write a test case for checking both with and without the empty columns. Check that the colspans are correct and that the empty columns appear.
  3. look for anything that has the <td>&nbsp;</td>.
  4. Fix colspan so that it ignores the empty columns.

Push everything to the develop branch and then do a pull request.

raredd commented 6 years ago

Doesn't this do it? I'm not positive how colspan needs to be fixed.

library('htmlTable')

ht <- ht0 <- htmlTable(
  head(mtcars),
  cgroup = c('', 'asdf', '', 'asdf'),
  n.cgroup = c(2,4,2,3)
)
ht <- gsub('<t[dh][^>]+?>&nbsp;</t[hd]>', '', ht)
ht
ht0