Nanov
Nanov

Reputation: 63

R markdown table loop NULL outcome

My code is producing a lot of NULLs at the end in the html output.

Could you please help me to prevent it?

---
title: "Test"
output: html_document
---

```{r warning=FALSE, message=FALSE, results = 'asis',echo=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)

 # nest all data except the cut column and create html tables
diamonds_tab <- diamonds %>%
  nest(-cut) %>%
  mutate(tab = map2(cut,data,function(cut,data){
      writeLines(landscape(kable_styling(kable(as.data.frame(head(data)),
          caption =cut,
          format = "html",align = "c",row.names = FALSE),
          latex_options = c("striped"), full_width = T)))
  }))

# print tab column, which contains the html tables    
invisible(walk(diamonds_tab$tab, print))
```

Upvotes: 1

Views: 101

Answers (2)

Nanov
Nanov

Reputation: 63

I found out, that it is only enough to remove the walk.

Upvotes: 1

Shafee
Shafee

Reputation: 19857

Instead of using invisible, wrap the print command with capture.output.

---
title: "Test"
output: html_document
---

```{r warning=FALSE, message=FALSE, results = 'asis',echo=FALSE}
library(tidyverse)
library(knitr)
library(kableExtra)

 # nest all data except the cut column and create html tables
diamonds_tab <- diamonds %>%
  nest(-cut) %>%
  mutate(tab = map2(cut,data,function(cut,data){
      writeLines(landscape(kable_styling(kable(as.data.frame(head(data)),
          caption =cut,
          format = "html",align = "c",row.names = FALSE),
          latex_options = c("striped"), full_width = T)))
  }))

# print tab column, which contains the html tables    
walk(diamonds_tab$tab, ~ capture.output(print(.x)))
```

Upvotes: 1

Related Questions