hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
321 stars 28 forks source link

Feature request : Equivalent of kable extra collapse_rows in huxtable #114

Closed GRnrsc closed 5 years ago

GRnrsc commented 5 years ago

mydata <- data.frame(C1 = c(rep("a", 10), rep("b", 5)), C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)), C3 = 1:15, C4 = sample(c(0,1), 15, replace = TRUE))

I would like to get a table as shown below.

image The table above was produced using the collapse_rows function in kable extra analyzes columns, finds out rows that can be grouped together, and make them multirow cells. However the resulting kable extra output table does not knit to word with formatting retained.

I have recently started working with markdown and knitr. The goal is to get (somewhat) complex tables and graphs to .pdf and also to .doc.

huxtable could construct the output shown above with the merge_cells(ht, c(min_row, max_row), c(min_col, max_col)) function, but in my case I don't know the position and number of rows to combine. In the tables I am trying to make in my work (excel pivot table like reports produced using dplyr) I need to find and group repeated values across multiple columns and I would find a collapse_rows like function in huxtable be very useful.

I thank you for huxtable as it seems to be the only way to get properly formatted tables into word, non interactively.

hughjonesd commented 5 years ago

Try merge_repeated_rows() in github master.

GRnrsc commented 5 years ago

Thanks very much.

It works very well.

huxdf %>% set_right_padding(10) %>% set_left_padding(10) %>% set_align(1:9, 2, 'right') %>% set_number_format(2) %>% rename("RANGE" = RANGE,"BLOCK" = BLOCK,"COUPETYPE" = COUPETYPE,"COMPARTMENT" = COMPARTMENT,"COUPE" = COUPE, "MAJOR SPECIES" = MAJOR.SPECIES,"30-60" = X1,"60-90" = X2,"90-120" = X3,"120-150" = X4,"> 150" = X5) %>% huxtable::add_colnames() %>%
set_caption('Stand Table, Compartment Wise Estimated Number of Stems') %>% merge_repeated_rows(col = c(1:4))