rr-markdownkablekableextra

Remove column name row in kable while using kable_styling


I'm trying to make a table without row names using kable, and I'm having an issue suppressing the line where column names go, but only if I'm using kable_styling (which I'm using to left-align the table). Is there maybe a separate tag within kable_styling that does this? Column names themselves are still removed, but the line they would go on is there. Code for both versions below.

library(knitr)
library(kableExtra)

df <- data.frame(x = 1:3,
                 y = c('A','B','C'))

df %>%
  kable(booktabs=TRUE,col.names=NULL) ## Removes column names and top line

df %>%
  kable(booktabs=TRUE,col.names=NULL) %>%
  kable_styling(position='left') ## Removes column names but not top line


Solution

  • You could add CSS to row 0 using rows_spec if your output is HTML.

    kable(df, booktabs = TRUE) |>
      kable_styling(position = 'left') |>
      row_spec(0, extra_css = "display: none;")
    

    res

    to remove all horizontal lines

    kable(df, booktabs = TRUE) |>
      kable_styling(position = 'left') |>
      row_spec(0, extra_css = "display: none;") |>
      row_spec(seq_len(nrow(df)), extra_css = "border-top: 0px;")
    

    blank tabke


    If you output to PDF, you can do

    kable(df, booktabs = TRUE, col.names = NULL) |>
      kable_styling(position = 'left') |>
      sub("\\\\toprule", "", x = _)
    

    pdf res