2

Consider this example

---
title: "R Notebook"
output:
  pdf_document: default
---

Hello this is a nice test

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = "latex")

library(knitr)
library(kableExtra)
library(dplyr)
# needed 
kable(cars, "latex") %>% kable_styling(bootstrap_options = "striped")
```

here we go

```{r, results = "asis"}
for (threshold in c(20, 25)) {
  cat("\n\n\\pagebreak\n")
  x <- mtcars %>%
    filter(mpg < threshold) %>%
    mutate(disp = cell_spec(
    disp, "latex", color = "white", bold = T,
    background = spec_color(1:10, end = 0.9, option = "A")
  )) %>% 
    kable('latex',  booktabs = T, escape=F) %>%
    kable_styling(latex_options = c("striped", "hold_position"),
                full_width = T) %>% 
   column_spec(1, bold = T, color = "red") %>% 
    add_header_above(c(" ", "$\\\\beta$" = 10),  escape = F)
  cat(x)
}
```

I am trying to use the trick explained here how to use a for loop in rmarkdown? to use all the raw power in kableExtrain my pdfs.

This is a pretty nice example of how to generate a dynamic rmarkdown document that creates tables in a for loop.

Nice indeed, except for the cell_specs gradient that contais some mysterious white spaces...

Any ideas?

enter image description here

Thanks!

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • In [one of the examples for `kableExtra`](http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf), it used `kable("latex", escape=F, ...)`, does that work? – r2evans Jul 09 '18 at 13:58
  • ha!! that works! perhaps you can post this as an aswer and add a few nice tricks? – ℕʘʘḆḽḘ Jul 09 '18 at 14:09
  • like titles with math formulas? – ℕʘʘḆḽḘ Jul 09 '18 at 14:13
  • using `add_header_above(c(" ", "$\beta$" = 5))` wont work here – ℕʘʘḆḽḘ Jul 09 '18 at 14:20
  • I don't know any tricks, sorry, I just googled and read the doc. I had used [`ztable`](https://cran.r-project.org/web/packages/ztable/index.html) for a similar purpose (before this was ready, perhaps? not sure ...), so I was curious how this worked. – r2evans Jul 09 '18 at 14:31
  • 1
    I noticed that you are trying to render both pdf and html. I will recommend you take off `"latex"` from your `kable` and `cell_spec`. `kableExtra` 0.9.0 will set the format automatically. – Hao Jul 09 '18 at 14:34
  • edited the question accordingly – ℕʘʘḆḽḘ Jul 09 '18 at 16:29
  • 1
    https://stackoverflow.com/questions/45409750/get-rid-of-addlinespace-in-kable – Hao Jul 09 '18 at 17:34

1 Answers1

1

If you're wondering about the whitespace every 5 rows, that is because booktabs=T. You should also add linesep="" to get rid of that whitespace.