Search code examples
cssrr-markdownkablekableextra

R remove header line after placing Kable side by side in Rmarkdown generated HTML


I'm using Rmarkdown to generate an HTML output, where two kables are placed side by side using kable::kables. However, there's an extra line above the side-by-side kables that I would like to remove. I've read the documentation of kableExtra, but seems like they didn't cover this issue.

The horizontal line that I would like to remove is indicated by the red arrow below.

output_HTML_with_kable_extra_line

Here's my Rmarkdown code for your reference:

---
title: "Untitled"
author: "benson23"
output:
  html_document:
    theme: default
    toc: yes
    toc_depth: 4
    toc_float: true
    code_folding: hide
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(rmarkdown)
library(kableExtra)
library(knitr)
```

## Help with Kable

As you can see, there's an extra line above the aligned tables, which I would like to remove.

```{r kable_extra_line}
df1 <- tibble(Dummy = c(90748, 91006, 90748, 91006), 
                       A = c(0, 6, 1, 110), 
                       B = c(8, 0, 339, 0), 
                       C = c(6, 7, 909, 309), 
                       D = c(0, 0, 0, 0))

df2 <- tibble(Dummy = c(90748, 91006, 90748, 91006), 
                       A = c(0, 901, 0, 50), 
                       B = c(0, 1, 122, 0), 
                       C = c(1847, 1, 754, 625), 
                       D = c(1, 0, 0, 0))
kables(list(
  kable(df1) %>%
    kable_styling(bootstrap_options = c("striped", "bordered"),
                  full_width = F) %>%
    row_spec(0, bold = T) %>%
    pack_rows("Pack some rows here", 1, 2, 
              label_row_css = "background-color: #666; color: #fff;") %>%
    pack_rows("Pack some rows here", 3, 4, 
              label_row_css = "background-color: #666; color: #fff;"),
  kable(df2) %>% 
    kable_styling(bootstrap_options = c("striped", "bordered"),
                  full_width = F) %>%
    row_spec(0, bold = T) %>%
    pack_rows("Pack some rows here", 1, 2, 
              label_row_css = "background-color: #666; color: #fff;") %>%
    pack_rows("Pack some rows here", 3, 4, 
              label_row_css = "background-color: #666; color: #fff;")
)) %>% 
  kable_styling() 
```

It'd be great if you have any idea on how to remove the line!


Solution

  • You can add the css .kable_wrapper { border: hidden;}:

    ---
    title: "Untitled"
    author: "benson23"
    output:
      html_document:
      theme: default
    toc: yes
    toc_depth: 4
    toc_float: true
    code_folding: hide
    ---
      
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(tidyverse)
    library(rmarkdown)
    library(kableExtra)
    library(knitr)
    ```
    
    <style>
    .kable_wrapper {
      border: hidden;
    }
    </style>
    
    ## Help with Kable
    
    As you can see, there's an extra line above the aligned tables, which I would like to remove.
    
    ```{r kable_extra_line}
        df1 <- tibble(Dummy = c(90748, 91006, 90748, 91006), 
                               A = c(0, 6, 1, 110), 
                               B = c(8, 0, 339, 0), 
                               C = c(6, 7, 909, 309), 
                               D = c(0, 0, 0, 0))
    
        df2 <- tibble(Dummy = c(90748, 91006, 90748, 91006), 
                               A = c(0, 901, 0, 50), 
                               B = c(0, 1, 122, 0), 
                               C = c(1847, 1, 754, 625), 
                               D = c(1, 0, 0, 0))
        kables(list(
          kable(df1) %>%
            kable_styling(bootstrap_options = c("striped", "bordered"),
                          full_width = F) %>%
            row_spec(0, bold = T) %>%
            pack_rows("Pack some rows here", 1, 2, 
                      label_row_css = "background-color: #666; color: #fff;") %>%
            pack_rows("Pack some rows here", 3, 4, 
                      label_row_css = "background-color: #666; color: #fff;"),
          kable(df2) %>% 
            kable_styling(bootstrap_options = c("striped", "bordered"),
                          full_width = F) %>%
            row_spec(0, bold = T) %>%
            pack_rows("Pack some rows here", 1, 2, 
                      label_row_css = "background-color: #666; color: #fff;") %>%
            pack_rows("Pack some rows here", 3, 4, 
                      label_row_css = "background-color: #666; color: #fff;")
        )) %>% 
          kable_styling() 
    ```