Get startedGet started for free

Adding word sources

1. Adding word sources

So far, the app has inputs for the optional word cloud parameters, but there is no way to change the data source. Let's learn about two new types of inputs that can be used to change the data for the word cloud.

2. Textarea inputs

Since the data argument of create_wordcloud is text, it could make sense to use a text input to capture text from the user and use it in the word cloud. But there would be an issue with this. Text inputs can only provide one line, or one row, so it's very inconvenient to enter a lot of text in it. The textAreaInput provides a larger text box, called a textarea, for this purpose. It's very similar to textinput, but it's great for long text because it has multiple rows and contains a vertical scrollbar. Textareas can even have an adjustable number of rows by specifying the `rows` argument.

3. File inputs - UI

Another way to supply your own words can be by uploading a text file instead of typing text into a textarea. Uploading files to a Shiny app is accomplished using the fileinput function. File inputs have the same two common parameters of all other inputs, inputId and label, as well as some other unique parameters that we're not going to use. Adding a file input to the UI of a shiny app results in this input showing up.

4. File inputs - UI

When the user clicks on the Browse button, a file selector appears and lets the user select a file from their computer. It's possible to allow the user to select multiple files instead of just one, or to restrict the user to only be able to choose a certain file type. You can look at the documentation of file input to see what other parameters can be used to customize the file selection, but we won't be using these options.

5. File inputs - server

After the user selects a file, that file gets uploaded to the computer that runs the Shiny app, and it becomes available to you in the server. But how exactly do you access this file? With other types of inputs, whenever the user modifies the input, the new value is available in the server through input-dollar-sign-input-id. You might expect input$inputId to give you access to the file that was uploaded, but that is not how file inputs work.

6. File inputs - server

Accessing the input value will give you a data-dot-frame that contains a few pieces of information about the file or files the user selected. Each row in the dataframe corresponds to one file. In this course we'll only use file inputs that accept single files, so there will only be one row. Each file has 4 pieces of information: name, size, type, and datapath. For example, if this is the dataframe returned from a file input, it means that the user selected a plain-text file called myfile-dot-txt that has a size of six thousands kilobytes. datapath is the most important variable - it tells you the file location of this uploaded file. This is not the location of the file on the user's computer, it's the location of the file on the computer that's running shiny, which means that shiny can access the file from this location. If you want to read the uploaded file, then input-dollar-sign-input-id-dollar-sign-datapath is what you use. For example if shiny expects a text file, you can use the readLines function with this datapath as the first argument.

7. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.