简体   繁体   中英

R markdown table loop NULL outcome

My code is producing a lot of NULL s 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))
```

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM