R - knitr: kable - delete individual headers and add one heading to multiple columns

R code in Rmarkdown file:

col1 <- c("dummydata","dummydata","dummydata")
col2 <- c("dummydata","dummydata","dummydata")  
col3 <- c("dummydata","dummydata","dummydata")
col4 <- c("dummydata","dummydata","dummydata")
col5 <- c("dummydata","dummydata","dummydata")
col6 <- c("dummydata","dummydata","dummydata")
col7 <- c("dummydata","dummydata","dummydata")
col8 <- c("dummydata","dummydata","dummydata")

df1 <- data.frame(col1,col2,col3,col4,col5,col6,col7,col8)

kable(df1, format="html",table.attr='class="myTable"') %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
add_header_above(c("Group1" = 2, "Group2" = 2,"Group3" = 2, "Group4" = 2))

Output: FlexDashBoard Kable - Dataframe as Table

ISSUE: I only need the headers in the combined columns, i.e. Group1, Group2, etc. And I want to remove the headers on separate columns, i.e. col1, col2, etc.

Is there a way to do this using dataframe, html / javascript, Rmarkdown, or any R package?

+1
source share
1 answer

You should be comfortable with regular expressions and the gsub function if you are more likely to encounter such problems.

here is the solution, the first line is your last line of code, reinforced by the character "x <-"

x <- kable(df1, format="html",table.attr='class="myTable"') %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
add_header_above(c("Group1" = 2, "Group2" = 2,"Group3" = 2, "Group4" = 2))

gsub("</th></tr><tr>.*</thead>","</thead>",x)

gsub : , , . (. *) , . , . , .

+1

Source: https://habr.com/ru/post/1679942/


All Articles