Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
810 views
in Technique[技术] by (71.8m points)

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

I'm trying to show a wordcloud2 wordcloud, but it only works in an html-knitted Rmd file. This works:

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

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

But this does not:

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

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

It will knit with always_allow_html: yes in the YAML, but the wordcloud doesn't show up:

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

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

I'm thinking maybe to save the figure as an image, then load it in the .Rmd, but that seems clumsy. Better ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way to do it, as I said, is to save as an image and load it in .Rmd. Not too bad actually:

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

The delay argument needs to be big enough to let the html fully render; if you watch a wordcloud2 generate, it takes a few seconds. 5 seconds seems enough for this, but you may have to increase it for bigger/more complex wordclouds, or if your computer is slow.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...