有没有办法在 Rmarkdown 中将 wordcloud2 显示为 PDF 或 Word 文件?

Is there any way to show a wordcloud2 in Rmarkdown as a PDF or Word file?

我正在尝试显示 wordcloud2 wordcloud,但它只能在 html-knitted Rmd 文件中使用。这有效:

---
title: "Untitled"
output: html_document
---

```{r wordcloud}
library(wordcloud2)
wordcloud2(demoFreq)
```

但这不是:

---
title: "Untitled"
output: pdf_document
---

```{r wordcloud}
library(wordcloud2)
wordcloud2(demoFreq)
```

它将与 always_allow_html 编织:在 YAML 中是的,但词云没有显示:

---
title: "Untitled"
output: pdf_document
always_allow_html: yes
---

```{r wordcloud}
library(wordcloud2)
wordcloud2(demoFreq)
```

我在想也许可以将图形保存为图像,然后将其加载到 .Rmd 中,但这看起来很笨拙。更好的想法?

正如我所说,一种方法是另存为图像并将其加载到 .Rmd 中。实际上还不错:

---
title: "Untitled"
output: pdf_document
---

```{r wordcloud}
library(wordcloud2)
library(webshot)
library(htmlwidgets)
my_graph <- wordcloud2(demoFreq, size = 1.5)
saveWidget(my_graph, "tmp.html", selfcontained = F)
webshot("tmp.html", "wc1.png", delay = 5, vwidth = 2000, vheight = 2000)
```
![wordcloud](wc1.png)

delay 参数需要足够大才能让 html 完全呈现;如果您观看 wordcloud2 生成,则需要几秒钟。 5 秒似乎足够了,但对于 bigger/more 复杂的词云,或者如果您的计算机速度较慢,您可能需要增加它。