Get startedGet started for free

Word clouds in Shiny

1. Word clouds in Shiny

A word cloud is a

2. Word clouds

visual representation of text data. A word that appears more times in a piece of text will appear as larger in the word cloud. This is an example of a word cloud of the US Constitution. The larger words, such as "president", "states", and "shall" appear the most times in the constitution, while the smaller words are used less often in the document. As you can see, word clouds are a useful tool for highlighting the most used words, and therefore most important words, in a text document.

3. Word clouds in R - function

Your friend, who really likes word clouds, wrote an R function, create-underscore-wordcloud, for generating them. Her function has 3 arguments. data is the text that will be used to count the words for the word cloud. You can either use a single string, or a list of strings. If you use a list, they'll be concatenated into one continuous text automatically. num_words is an optional argument that tells the word cloud how many words to show. The background argument is also optional and can be used to add a background color. The default is white.

4. Word clouds in R - usage

Here's an example usage of your friend's create_wordcloud function. Let's assume that we have the entire text of the US constitution saved in a variable called us-underscore-constitution. If we call the create_wordcloud function with this text, and set num_words to 15 and the background to yellow, R will generate this word cloud.

5. Word clouds: from R to Shiny

How does this relate to shiny? Well, your friend really wants to share this function with all her friends, but not all of them know how to use R, so they can't use it. But, thanks to Shiny, you can help. You can build a Shiny app that uses her function to let people create their own word clouds. This will allow all her friends, even the ones who are unfamiliar with R, to generate word clouds using a point-and-click interface instead of using the R command line. This is actually a common use of shiny: it's great for taking a function that you, or someone else, wrote and exposing it through a graphical interface to make it easier to share with other people.

6. Word clouds in Shiny

This is the app you'll build in this chapter. The parameters of the create_wordcloud function become inputs in the shiny app, so manipulating the inputs is equivalent to calling the word cloud function with different parameters. The graphic output of the function, which is the word cloud itself, becomes an output in the shiny app. We've seen a few outputs before, like tables and plots, but word clouds are a new type of output. They use word-cloud-2-output as their output placeholder in the UI, and render-word-cloud-two in the server.

7. Let's practice!

Now let's go and build this app.