我正在尝试展示wordcloud2 wordcloud,但它只适用于html编织的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中的是,但是wordcloud没有出现:
---
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秒似乎足够了,但你可能不得不为更大/更复杂的wordcloud增加它,或者如果你的计算机很慢。