Search code examples
rr-markdownknitrkableextra

How to change appearance of specific columns of two tables listed side by side (r markdown)


I've got two tables with identical variables but different observations and I've formatted them to sit next to each other side by side to give the appearance of one table using,

knitr::kable(list(df1, df2))

(Apologies I can't show an image as data is confidential, ask me to elaborate further if I'm not making sense)

And I want to make the first column of the first table bold and I tried using

knitr::kable(list(df1, df2)) %>%
    kable_paper("striped", full_width = F) %>%
  column_spec(1, bold = T)

to do this but that just made the entire first table bold. Bare with me I'm very new to rmarkdown, any help would be greatly appreciated


Solution

  • Here is one way if you are trying to knit the file in HTML.

    ---
    title: "temp"
    output: html_document
    ---
    
    ```{r, echo = FALSE, warning=FALSE, message=FALSE}
    library(knitr)
    library(kableExtra)
    
    df1 <- data.frame(col1 = letters[1:5], col2 = rnorm(5))
    df2 <- data.frame(col1 = letters[11:15], col2 = rnorm(5))
    
    kable(df1) %>%
        kable_paper("striped", full_width = FALSE) %>%
        kable_styling(full_width = FALSE, position = "float_left") %>%
        column_spec(1, bold = TRUE)
    
    kable(df2) %>%
        kable_paper("striped", full_width = FALSE) %>%
       kable_styling(full_width = FALSE, position = "left")
    ```
    

    enter image description here