R - kable() used in .Rmd does not show output in notebook
Asked Answered
K

2

7

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")

```
Kevel answered 11/4, 2018 at 12:30 Comment(0)
B
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")
```
Barbuto answered 11/4, 2018 at 13:33 Comment(3)
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?Kevel
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...Barbuto
You should be able to set it automatically by accessing the Rmarkdown metadata in your code chunk: #31221440Penates
S
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.

Synecious answered 3/3, 2021 at 2:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.