Rmarkdown中的Wordcloud错误:HTML中仅显示一个wordcloud

时间:2019-12-22 09:31:21

标签: r r-markdown word-cloud wordcloud2

我通过导入两个不同的数据集在Rmarkdown中成功创建了两个wordcloud,但是将其编织为html后没有出现一个wordcloud。有人知道如何解决吗?


输出: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 个答案:

答案 0 :(得分:0)

这是一个完全可复制的工作示例,该示例将结果写入图像文件,然后查看它们。我使用dput(head(s1))来获取示例数据。注释掉您的环境。

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