A word-cloud generator with R
In Corpus Linguistics and Statistics with R, I showed how to make a word cloud from a text file (Section 6.3). A word cloud is a user-friendly way of representing a frequency list graphically. I have decided to share the code here in the form of a Shiny app. Shiny is a web application that is created using the shiny package in R. Shiny allows users to create interactive web apps quickly and easily using R, which makes it a popular choice for data scientists who want to share their work with others. Shiny apps are often used to visualize data and create user-friendly interfaces for data exploration.
Shiny 101
To make a Shiny app, you will need to have the shiny package installed in R. To install the Shiny package, open R and type the following:
install.packages("shiny")
Once the shiny package is installed, you can create a new Shiny app by using the shinyApp() function. This function takes two arguments: the first is the UI (user interface) of the app, and the second is the server function that defines the behavior of the app.
The user interface (UI) defines the layout and appearance of the app. The UI typically consists of a combination of input elements, such as buttons, checkboxes, and text boxes, that allow the user to interact with the app, and output elements, such as graphs and tables, that display the results of the app’s computations.
The server part is of the script contains instructions that the computer should follow to build the app. These instructions typically include instructions for reading in data, performing computations, and generating output. The server script also includes instructions for how the app should respond to user input, such as by updating the output or changing the plot in response to a button click.
Here is an example of a simple Shiny app that displays “Hello, Shiny!” on the screen:
# Load the Shiny package
library(shiny)
# Define the UI
ui <- fluidPage(
# Add a title to the page
title = "Hello, Shiny!",
# Add a main panel to the page
mainPanel(
# Add a text output to the main panel
textOutput("hello")
)
)
# Define the server function
server <- function(input, output) {
# Add a reactive expression that returns the string "Hello, Shiny!"
output$hello <- reactive({
"Hello, Shiny!"
})
}
# Create the Shiny app by combining UI and server
shinyApp(ui = ui, server = server)
To run the app, you have two options. The first option is to copy and paste the whole script into R. The second option is to save the above script as an R file, e.g., myshinyapp.r and use the runApp() function. This function takes the file path of the script as its argument, and runs the code in the script:
library(shiny)
runApp("/path/to/myshinyapp.R")
This will open the Shiny app in your default web browser, where you can interact with it.
The Shiny word-cloud app
Save the following script into an R file: wordcloudapp.R.
# Load required packages
library(shiny)
library(tm)
library(wordcloud)
# Define UI
ui <- fluidPage(
# Application title
titlePanel("Word Cloud Generator"),
# Sidebar with options to load a text file and specify number of words
sidebarLayout(
sidebarPanel(
fileInput("file", "Choose a text file", accept = c("text/plain", ".txt")),
sliderInput("num_words", "Number of words to include in word cloud:",
min = 50, max = 500, value = 100)
),
# Show the word cloud in the main panel
mainPanel(
plotOutput("wordcloud", width = "600px", height = "600px")
)
)
)
# Define server logic
server <- function(input, output) {
# Reactive function to process the text file
process_text <- reactive({
# Load the text file
text <- readLines(input$file$datapath)
# Convert the text to lowercase and remove punctuation
text <- tolower(text)
text <- gsub("[[:punct:]]", "", text)
# Tokenize the text
text <- unlist(strsplit(text, "\\s+"))
# Return the processed text
return(text)
})
# Generate the word cloud using the processed text
output$wordcloud <- renderPlot({
wordcloud(process_text(), max.words = input$num_words)
})
}
# Create the Shiny app by combining UI and server
shinyApp(ui = ui, server = server)
Load the script as follows:
runApp("path/to/file/wordcloudapp.R")
The Shiny app opens in your default web browser. At first, you see an error message (‘Error: ‘con’ is not a connection’). This is because you have not loaded a text yet.

Let us load Herman Melville’s Moby Dick, which I downloaded from Project Gutenberg. It is a UTF-8 text file that I post-processed with R. To do so, click on ‘Browse’. This opens up an interactive window. Look for the text file and click ‘Open’.

The size of each word is indexed on its type frequency. By default, the number of words in the word cloud is 100. Using the ruler, we set the number of words to be included in the word cloud to 300.

The word cloud is pretty basic but does the trick. If you read the documentation of the wordcloud package, you will discover more features, such as coloring the words based on how often they occur in the text. You can also customize the stoplist (the list of words that are excluded from the word cloud, such as function words).
Have fun!
The text only may be used under licence Creative Commons Attribution Non Commercial 4.0 International. All other elements (illustrations, imported files) are “All rights reserved”, unless otherwise stated.
OpenEdition suggests that you cite this post as follows:
Guillaume Desagulier (December 10, 2022). A word-cloud generator with R. Around the word. Retrieved May 21, 2026 from https://doi.org/10.58079/n4va


orcid.org/0000-0003-4895-0788