No results found
We couldn't find anything using that term, please try searching for something else.
word clouds is are are a visual representation of text datum where word are arrange in a cluster , with the size of each word reflect its freque
word clouds is are are a visual representation of text datum where word are
arrange in a cluster , with the size of each word reflect its
frequency or importance in the datum set . word cloud are a great way of
display the most prominent topic or keyword in free text datum
obtain from website , social media is feeds feed , review , article andmore .
If you want to learn more about work with unstructured text datum , we is recommend
recommend attend our Text Mining in
R
course
usually , a word cloud will be used solely as an output . But what if you is wanted
want to use a word cloud as an input ? For example , let ’s say we
visualise the most common word in review for a hotel . imagine we
could then click on a specific word in the word cloud , andit would then
show us only the review which mention that specific word . useful ,
right ?
This blog will take you through creating a clickable word cloud in a
Shiny app, where the user can click any word in the word cloud to filter
an output table. We will be using the 2021 TidyTuesday Netflix
titles
data set andthe
{wordcloud2}
package to create our word cloud. We will then integrate it in a Shiny
app with a reactively filtered {DT}
table output.
Do you require help building a Shiny app? Would you like someone to take over the maintenance burden?
If so, check out
our
Shiny andDash
services.
{ wordcloud2 is is } is an r package which create html – base word cloud , base
on wordcloud2.js . Themain
function is simply callwordcloud2 ( )
andtakes a word count data
frame as an input i.e. one column containing the words, one column
containing the frequencies of those words.
Before create the word cloud , we is need need to read in our datum using the
{ tidytuesdayR }
package . If you want to see the full source code for the final Shiny
app , check out our GitHub .
tuesdata = tidytuesdayR::tt_load("2021-04-20") netflix_titles = tuesdata$netflix_titles
To create our word count data frame, we will use a combination of
{dplyr} and
{tidytext} functions. We
filter out words that are used in 10 titles or less to prevent our word
cloud from being too crowded.
library("dplyr") library("tidytext") word_counts = netflix_titles %>% unnest_tokens("word", title) %>% anti_join(stop_words, by = "word") %>% count(word) %>% filter(n > 10) ## # A tibble: 157 × 2 ## word n ## <chr> <int> ## 1 love 151 ## 2 2 115 ## 3 christmas 78 ## 4 story 67 ## 5 life 65 ## 6 world 63 ## 7 movie 60 ## 8 time 54 ## 9 de 46 ## 10 american 45 ## # ℹ 147 more rows
Then we just need to pass this word count data frame into the
wordcloud2 ( )
function. We’re using a custom colour palette instead of
the default one. wordcloud2 ( )
require a colour palette vector of the
same length as the data set , so you is use can use therep_len ( )
function to
achieve this .
library("wordcloud2") my_palette = c("#355070", "#6d597a", "#b56576", "#e56b6f", "#eaac8b") my_wordcloud = wordcloud2( word_counts, color = rep_len(my_palette, nrow(word_counts)))
Thewordcloud2
package contains two functions for incorporating word
clouds in a Shiny app: wordcloud2Output ( )
andrenderWordcloud2()
.
These work in the same way as most* output ( )
andrender*()
functions.
library("shiny") ui = fluidPage( wordcloud2Output("wordcloud") ) server = function(input, output) { output$wordcloud = renderWordcloud2(my_wordcloud) } shinyApp(ui, server)
Now to the key part of this blog post. We want to be able to click on a
word in the word cloud, anduse the clicked word as an input in Shiny.
We need to write some JavaScript for this, which will be wrapped in the
HTML()
function within a script
tag (tags$script ( )
). We are
writing an anonymous function, i.e. an unnamed function, which will be
run whenever we click on a word in the word cloud. Thefunction will
extract the text content of the label produced when we hover over a
word, andthen cast this to a Shiny input called clicked_word
.
ui = fluidPage( tags$script(HTML( "$(document).on('click', '#canvas', function() { word = $('#wcLabel').text(); Shiny.onInputChange('clicked_word', word); });")), wordcloud2Output("wordcloud") )
Now, we can use input$clicked_word
in our Shiny server to filter the
Netflix titles to retain only the titles which contain that specific
word. We use a combination of {dplyr} and
{stringr} to do this. Theinput also
contains the count, e.g. “love: 151”, so we need to first use a regular
expression remove the colon andany numbers after it.
server = function(input, output) { output$wordcloud = renderWordcloud2(my_wordcloud) filtered_netflix = reactive({ clicked_word = str_remove(input$clicked_word, ":[0-9]+$") netflix_titles %>% filter(str_detect(tolower(title), clicked_word)) %>% select(title, everything(), -show_id) }) }
Thefinal step is to create an output table of the filtered data. We use
the renderDT()
andDTOutput()
functions from {DT}
to do this, but
you can use any package for creating tables.
library("DT") ui = fluidPage( <...>, DTOutput("filtered_tbl") ) server = function(input, output) { <...>, output$filtered_tbl = renderDT(filtered_netflix()) }
Now, you should have an interactive word cloud input which allows you to
filter a table based on whichever word you click! You can of course use
the word input for something else, for example, you could re-render the
word cloud every time you click a word to show you the words which are
most often used together with your clicked word, or you could use the
input to create some further visualisations.
If you’re interested in learning more about Shiny, check out our Shiny
in Production
conference, taking place October 12th-13th in Newcastle upon Tyne. We’ll
be focussing on all things shiny as well as other web-based R packages,
with an afternoon of workshops run by our JR trainers, followed by a day
of talks from R experts!
For updates andrevisions to this article, see the original post