3

我正在尝试显示 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)
```

它会在 YAML 中使用 always_allow_html: yes 编织,但 wordcloud 不会出现:

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

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

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

4

1 回答 1

7

正如我所说,一种方法是保存为图像并将其加载到 .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,或者如果您的计算机速度很慢,您可能需要增加它。

于 2017-12-16T23:44:46.883 回答