6

I just started using kableExtra library to make my tables look better in the PDF output.
But when I use kable() function in R Notebook file, it does not show the output. Instead I see a large white space where the output should be.
Here is a screenshot:

enter image description here When I Knit the file to PDF I can see the output.
Here is a screenshot:
enter image description here
Is there a way I can make the output appear both in the Notebook and PDF? Here is my code:

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

```{r  message=FALSE, warning=FALSE}
library(knitr)
library(kableExtra)
library(dplyr)
#plot(cars)
```

```{r}
 cars %>% 
  slice(1:10) %>% 
  select(speed, dist) %>% 
  kable(format = "latex", booktabs = T) %>% 
  column_spec(column = 1:2, width = "0.5in")

```
jmich738
  • 1,565
  • 3
  • 24
  • 41

2 Answers2

6

You have to set a different kable format parameter for each output and specify results = 'asis' in chunk options.

For HTML / Notebook:

```{r, results='asis'}
cars %>% 
  slice(1:10) %>% 
  select(speed, dist) %>% 
  kable(format = "html", booktabs = T) %>% 
  column_spec(column = 1:2, width = "0.5in")
```

For PDF:

```{r, results='asis'}
cars %>% 
  slice(1:10) %>% 
  select(speed, dist) %>% 
  kable(format = "latex", booktabs = T) %>% 
  column_spec(column = 1:2, width = "0.5in")
```
Bruno Pinheiro
  • 964
  • 8
  • 20
  • So there is no way of have both without making changes to that option? When I change format to `format = "html"` , I don't see the `kable()` table that I expect. It looks differently to the way it looks in PDF. It looks very plain without the lines. Is that normal behaviour? – jmich738 Apr 11 '18 at 13:48
  • 1
    Yes, there's no way to automatic set `kable()` by output format. At least i don't know any method. And yes, too, it's a normal behavior. Tables will be different formated in PDF and HTML. Maybe you should look at other tables packages like `xtable` or `stargazer`... – Bruno Pinheiro Apr 11 '18 at 19:35
  • You should be able to set it automatically by accessing the Rmarkdown metadata in your code chunk: https://stackoverflow.com/questions/31220940/how-to-access-yaml-metadata-from-knitr – Patrick B. Jun 19 '19 at 03:31
4

I was having a similar problem, but it turns out that my editor theme's white default text was making the font in the .Rmd output invisible (but I could still highlight it).

My kable output was not working inside the .Rmd file--but was working fine when running the code in the console, and also when I knit the file. I was using the Idle Fingers editor theme (sort of a 'dark mode') and changing this to another theme fixed the issue.

nickjf6
  • 69
  • 4