Wordcloud error in Rmarkdown: only one wordcloud shows up in html

1.2k Views Asked by At

I successfully created two wordclouds in Rmarkdown by importing two different datasets, but one wordcloud did not show up after I knit it to html. Does anyone know how to fix it?


output: html_document

knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE)
library(wordcloud2)
library(here)
library(rio)

WordCloud 1

s1 <- import(here("data", "set1.xlsx"))
wordcloud2(s1, color = "random-light", backgroundColor = "dark")

WordCloud 2

s2 <- import(here("data", "set2.xlsx"))
wordcloud2(s2, color = "random-light", backgroundColor = "dark")
1

There are 1 best solutions below

2
On

Here's a fully reproducible working example that writes the results to image files then views them. I used dput(head(s1)) to get the example data. Comment out for your environment.

---
title: "WordCloud_Trial"
output: html_document
---

```{r global_options, include=FALSE}
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, message=FALSE)
```

```{r}
library(wordcloud2)
library(here)
library(rio)
library(webshot)
library(htmlwidgets)
```

## WordCloud 1
```{r echo=FALSE, message=FALSE, include=F}
#s1 <- import(here("data", "set1.xlsx"))
s1 <- structure(list(Type = c("provide", "include", "develop", "support", 
"create", "suggest"), `Token Frequency` = c(1218, 1095, 1088, 
1078, 1005, 988)), row.names = c(NA, 6L), class = "data.frame")
w1 <- wordcloud2(s1, color = "random-light", backgroundColor = "dark")
saveWidget(w1, '1.html', selfcontained = F)
webshot('1.html', '1.png', vwidth=700,vheight=500, delay = 5)
```
![](1.png)

## WordCloud 2
```{r echo=FALSE, message=FALSE, include=F}
#s2 <- import(here("data", "set2.xlsx")) 
s2 <- structure(list(Type = c("decrease", "cope", "accommodate", "confirm", 
"reinforce", "advance"), `Token Frequency` = c(251, 240, 232, 
229, 228, 227)), row.names = c(NA, 6L), class = "data.frame")
w2 = wordcloud2(s2, color = "random-light", backgroundColor = "dark")
saveWidget(w2, '2.html', selfcontained = F)
webshot('2.html', '2.png', vwidth=700,vheight=500, delay = 5)
```
![](2.png)