Search code examples
r-markdownxtable

Xtable grey rows overwritting vertical lines


    ---
title: "Title"
author: ''
date: ''
output:
 pdf_document:
  template: default.tex
geometry: top=0.5cm, bottom=0.5cm, left=0.5cm, right=0.5cm
header-includes: null
fontsize: 4pt
classoption: portrait
sansfont: Calibri Light
---
#Name1: `r "Name1"`  
#Name2: `r "Name2"`

```{r,  echo=FALSE,  message=FALSE, warning=FALSE, results='asis'}
df <- mtcars
n = nrow(df)
hlines=c(-1,0,(n-1),n)
my_align = "c|c|c|ccccc|ccc|c|"
rws <- seq(1, (n-1), by = 2)
col <- rep("\\rowcolor[gray]{.90} ", length(rws))
xtable::print.xtable(xtable(df
, align = my_align)
, add.to.row = list(pos = as.list(rws), command = col)
, booktabs = F
, hline.after = hlines, type = "latex") 
```

I am using an Rmarkdown to print a table which has a lot of formatting. When I add the add.to.rwo part to get grey and white alternate rows the vertical lines are removed in the grey rows.

How do I correct this? It is very difficult to create a reproducible example but hopefully the same problem will apply to any df (with the correct Latex packages behind it)

Thanks :)


Solution

  • Try comparing these two tables. The first is your table as you coded it, the second is done by pixiedust with the hhline option set to TRUE.

    ---
    title: "Title"
    author: ''
    date: ''
    output:
     pdf_document:
    geometry: top=0.5cm, bottom=0.5cm, left=0.5cm, right=0.5cm
    header-includes: 
    - \usepackage{amssymb} 
    - \usepackage{arydshln} 
    - \usepackage{caption} 
    - \usepackage{graphicx} 
    - \usepackage{hhline} 
    - \usepackage{longtable} 
    - \usepackage{multirow} 
    - \usepackage[dvipsnames,table]{xcolor} 
    fontsize: 4pt
    classoption: portrait
    sansfont: Calibri Light
    ---
    #Name1: `r "Name1"`  
    #Name2: `r "Name2"`
    
    ```{r,  echo=FALSE,  message=FALSE, warning=FALSE, results='asis'}
    library(xtable)
    df <- mtcars
    n = nrow(df)
    hlines=c(-1,0,(n-1),n)
    my_align = "c|c|c|ccccc|ccc|c|"
    rws <- seq(1, (n-1), by = 2)
    col <- rep("\\rowcolor[gray]{.90} ", length(rws))
    xtable::print.xtable(xtable(df
    , align = my_align)
    , add.to.row = list(pos = as.list(rws), command = col)
    , booktabs = F
    , hline.after = hlines, type = "latex") 
    ```
    
    ```{r}
    library(pixiedust)
    dust(df,
         hhline = TRUE,
         keep_rownames = TRUE) %>%
      medley_bw() %>%
      sprinkle_colnames(.rownames = "") %>%
      sprinkle(cols = c(".rownames", "mpg", "cyl", "qsec", "gear", "carb"),
               border = "right") %>%
      sprinkle(rows = nrow(mtcars),
               border = "top") %>%
      sprinkle(bg_pattern_by = "rows")
    ```