Get startedGet started for free

Combining all the word sources

1. Combining all the word sources

So far, the create_wordcloud function

2. Combining all the word sources

has been used with three different types of data. A pre-defined text document, in the form of the artofwar variable. A user-supplied list of words, using textarea inputs. And a text file that the user can upload using file inputs. Instead of only allowing one of these to work, the next natural step is to let the user decide which of these options to use. Radio buttons are a great fit for this task - we can use them to ask the user to choose which data source to use.

3. Radio buttons - review

We've already seen radio buttons before. One of the examples we used was this input that asks you for your favorite time of day. As you may recall, radio button inputs have a choices argument that accepts a vector of choices to present to the user. In this case, choices has the values morning, afternoon, and evening, so these are the values shown to the user.

4. Radio buttons - advanced

One useful feature of radio buttons is that it's possible to change the way an option is shown to the user without affecting the actual value of the input. This can be useful if you want to show user-friendly values to the user, but when accessing the input from the server you'd rather have a more R-friendly value, for example one without spaces or special characters. To do this, you add names to each value in the `choices` argument. The name of each choice becomes the text that the user sees. But this only affects what the user sees. The value of the input, when you access it in the server, will remain unchanged. In this example, when retrieving the input value, Shiny will return the text "Afternoon" when the user selects the phrase "Love my afternoons".

5. Conditional panels

One feature of Shiny we haven't discussed yet is conditional panels. Conditional panels are used in the UI when you want to show or hide specific UI elements based on the value of some input. The first argument of the conditionalPanel function is `condition`. The condition usually involves checking or comparing the value of an input. When writing the condition, if you want to refer to an input value, instead of input-dollar-sign-id, you use input-dot-id. After the first argument of the conditional panel, you can place as many UI elements as you want. This can be text, inputs, outputs -- anything that goes inside fluidPage can go here. These UI elements will be shown when the condition is met, and when the condition is not true these UI will be hidden.

6. Conditional panels

For example, suppose this is the UI of your app. There are radio buttons, the same ones from before that ask about your favorite time of day, and there's a plot. Currently the plot is always visible, but what if it's only relevant when the user selects the "Morning" radio button? This kind of control can be achieved with a conditional panel. The plot gets wrapped inside a conditionalPanel function, with a condition checking if input-dot-time-of-day is equal to Morning. Now if any radio button other than "morning" is selected, the condition becomes false, and so the plot gets hidden.

7. Let's practice!