【问题标题】:Wordcloud error in Rmarkdown: only one wordcloud shows up in htmlRmarkdown 中的 Wordcloud 错误:html 中只显示一个 wordcloud
【发布时间】:2020-04-14 00:26:23
【问题描述】:

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


输出:html_document

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

词云 1

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

词云 2

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

【问题讨论】:

  • 没有数据无法回答
  • 您能否提供一个带有示例数据的可重现示例?
  • @awchisholm 感谢您指出这一点。我在github.com/hshung/WordCloud_trial.git 创建了一个示例仓库
  • 请提供一个最小的自包含示例。如果重现问题的工作不需要克隆外部 git 存储库或手动下载数据,您将更有可能得到答案。

标签: r r-markdown word-cloud wordcloud2


【解决方案1】:

这是一个完全可重现的工作示例,它将结果写入图像文件然后查看它们。我使用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)

【讨论】:

  • 感谢您的帮助!我试图重现数据,但结果没有产生任何图像文件。它在 ![](1.png) 和 ![](2.png) 的路径上没有显示图像。 @awchisholm
  • 你安装了 webshot 和 htmlwidgets 包吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
相关资源
最近更新 更多