Change the word cloud parameters
Recall that create_wordcloud()
has two optional arguments: num_words
, which is an integer specifying the maximum number of words to draw, and background
, which specifies the background color of the image.
Right now, the Shiny app simply outputs a word cloud with the exact same parameters all the time. Since the word cloud generating function accepts these two parameters, it would be wasteful not to use them. The parameters should be adjustable by the user using Shiny inputs.
Your task is to add two inputs to the Shiny app, and use the values from these inputs as the num_words
and background
parameters of the word cloud.
This exercise is part of the course
Case Studies: Building Web Applications with Shiny in R
Exercise instructions
All the required packages, including colourpicker
, have been loaded to your workspace. Specifically:
- Add a numeric input with an ID of "num", a default value of 100, and minimum value of 5.
- Add a color input (from the
colourpicker
package) with an ID of "col", a label of "Background color", and a default color of "white". - Use the values from the two inputs to set the parameters of
create_wordcloud()
(line 16).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
ui <- fluidPage(
h1("Word Cloud"),
# Add a numeric input for the number of words
___(inputId = ___, label = "Maximum number of words",
value = ___, min = ___),
# Add a color input for the background color
___(___),
wordcloud2Output("cloud")
)
server <- function(input, output) {
output$cloud <- renderWordcloud2({
# Use the values from the two inputs as
# parameters to the word cloud
create_wordcloud(artofwar,
num_words = ___, background = ___)
})
}
shinyApp(ui = ui, server = server)