asosnovsky asosnovsky - 4 years ago 241
LaTeX Question

R-Markdown: Modify the default table output for PDF

This is really me, getting really confused and being unable to find anything anywhere in terms of documentation about this.

The following applies to Rmd as its used in Rstudio.

So say this table:

Table Header | Second Header
------------- | -------------
Table Cell | Cell 2
Cell 3 | Cell 4


When it gets compiled to latex (or pdf)
the output is this

enter image description here

But what I want it to be is more of this:

enter image description here

or like this

enter image description here

i.e. include the vertical line or horizontal line.

Now, when I compile it to html, I can fix up some of the styles by including my own css file. Is there anything of the sort that I can do with LaTEX output?

Answer Source

I don't know how to do this using markdown alone, and it probably involves creating some sort of template. I've never pursued such a solution, so I can't really say if it is or isn't possible. I've pursued other solutions that will generate the necessary HTML of LaTeX code for me. The code below is a working RMD that will knit to both a PDF and an HTML file.

---
title: "Untitled"
output:
  pdf_document: null
  html_document: null
header-includes:
- \usepackage{amssymb}
- \usepackage{arydshln}
- \usepackage{caption}
- \usepackage{graphicx}
- \usepackage{hhline}
- \usepackage{longtable}
- \usepackage{multirow}
- \usepackage[dvipsnames,table]{xcolor}
---

```{r}
library(pixiedust)
options(pixie_float = FALSE)

DF <- data.frame(table_header = c("Table Cell", "Cell 3"),
                 second_header = c("Cell 2", "Cell 4"),
                 stringsAsFactors = FALSE)

Base <- 
  dust(DF) %>%
  medley_bw() %>% mimics the default bordering of standard markdown table
  sprinkle_colnames("Table Header", "Second Header") %>%
  sprinkle_table(pad = 3) # padding for HTML tables

# print the table with vertical line between columns
Base %>%
  sprinkle_table(cols = 1,
                 border = "right")

# print the table with horizontal line between rows
Base %>%
  sprinkle(rows = 1,
           border = "bottom")
```

The resulting tables have the following appearance

Table with vertical column divider

Table with horizontal row divider

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download