Add a layout
As you have seen in previous chapters, using a layout in a Shiny app is important in order to organize the interface and make it easier to use.
The app currently has very few objects (one title, two inputs, one word cloud output) so it is still manageable without a layout. However, the app is going to grow in the next exercises and having a sidebar layout will be beneficial. It's a good idea to add a layout to your app earlier rather than later, because placing new Shiny UI elements into an existing layout is easier than rearranging a larger non-structured app later.
As is commonly done with Shiny apps and other interactive applications, the inputs will be kept in the smaller sidebar, while the main output (the word cloud) will be in the larger main panel.
This exercise is part of the course
Case Studies: Building Web Applications with Shiny in R
Exercise instructions
Your next task is to add a sidebar layout to the current Shiny app. No new UI elements are to be added other than the structure for the layout. Specifically:
- Add a sidebar layout to the UI.
- Inside the sidebar layout, define a sidebar panel to house the two inputs.
- Also inside the sidebar layout, define a main panel that contains the word cloud output (line 12).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
ui <- fluidPage(
h1("Word Cloud"),
# Add a sidebar layout to the UI
___(
# Define a sidebar panel around the inputs
___(
numericInput("num", "Maximum number of words",
value = 100, min = 5),
colourInput("col", "Background color", value = "white")
),
# Define a main panel around the output
___(
wordcloud2Output("cloud")
)
)
)
server <- function(input, output) {
output$cloud <- renderWordcloud2({
create_wordcloud(artofwar,
num_words = input$num, background = input$col)
})
}
shinyApp(ui = ui, server = server)