Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

```
like image 276
jmich738 Avatar asked Apr 11 '18 12:04

jmich738


People also ask

What does the Kable function do in R?

The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.

What package is Kable in R?

The kableExtra package (Zhu 2021) is designed to extend the basic functionality of tables produced using knitr::kable() (see Section 10.1).

How do I insert a table in R markdown?

Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.

Does Kable() function show output in R notebook?

latex - R - kable() used in .Rmd does not show output in notebook - Stack Overflow 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... Stack Overflow About Products For Teams

What are the problems with kableextra in R?

1 Suppressing lengthy kable tables in Rmd inline output while retaining in final PDF 2 kableExtra: Vertical alignment not working in PDF output with many columns 2 Math symbols not rendered correctly in R notebook format from KableExtra table objects 2

What format does Kable use for tables?

For R Markdown documents, kable () uses the pipe format for tables by default, which looks like this: You can also generate simple tables, or tables in HTML, LaTeX, and reStructuredText: Please note that only the formats pipe and simple are portable, i.e., they work for any output document format.

What format does Kable () use in R Markdown?

For R Markdown documents, kable () uses the pipe format for tables by default, which looks like this: knitr::kable(head(mtcars 1:4]), "pipe")


2 Answers

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")
```
like image 193
Bruno Pinheiro Avatar answered Oct 02 '22 23:10

Bruno Pinheiro


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.

like image 28
nickjf6 Avatar answered Oct 02 '22 23:10

nickjf6