A simple word cloud generator, based on this blog post by PirateGrunt.
Last active
January 17, 2018 11:46
-
-
Save jcheng5/9496297 to your computer and use it in GitHub Desktop.
Word CloudLicense: MIT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Title: Word cloud | |
Author: Fereshteh Karimeddini <[email protected]> | |
AuthorUrl: http://www.rstudio.com/ | |
License: MIT | |
DisplayMode: Showcase | |
Tags: wordcloud text-mining actionbutton | |
Type: Shiny |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tm) | |
library(wordcloud) | |
library(memoise) | |
# The list of valid books | |
books <<- list("A Mid Summer Night's Dream" = "summer", | |
"The Merchant of Venice" = "merchant", | |
"Romeo and Juliet" = "romeo") | |
# Using "memoise" to automatically cache the results | |
getTermMatrix <- memoise(function(book) { | |
# Careful not to let just any name slip in here; a | |
# malicious user could manipulate this value. | |
if (!(book %in% books)) | |
stop("Unknown book") | |
text <- readLines(sprintf("./%s.txt.gz", book), | |
encoding="UTF-8") | |
myCorpus = Corpus(VectorSource(text)) | |
myCorpus = tm_map(myCorpus, tolower) | |
myCorpus = tm_map(myCorpus, removePunctuation) | |
myCorpus = tm_map(myCorpus, removeNumbers) | |
myCorpus = tm_map(myCorpus, removeWords, | |
c(stopwords("SMART"), "thy", "thou", "thee")) | |
myDTM = TermDocumentMatrix(myCorpus, | |
control = list(minWordLength = 1)) | |
m = as.matrix(myDTM) | |
sort(rowSums(m), decreasing = TRUE) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(shinyIncubator) | |
# Text of the books downloaded from: | |
# A Mid Summer Night's Dream: | |
# http://www.gutenberg.org/cache/epub/2242/pg2242.txt | |
# The Merchant of Venice: | |
# http://www.gutenberg.org/cache/epub/2243/pg2243.txt | |
# Romeo and Juliet: | |
# http://www.gutenberg.org/cache/epub/1112/pg1112.txt | |
shinyServer(function(input, output, session) { | |
# Define a reactive expression for the document term matrix | |
terms <- reactive({ | |
# Change when the "update" button is pressed... | |
input$update | |
# ...but not for anything else | |
isolate({ | |
withProgress(session, { | |
setProgress(message = "Processing corpus...") | |
getTermMatrix(input$selection) | |
}) | |
}) | |
}) | |
# Make the wordcloud drawing predictable during a session | |
wordcloud_rep <- repeatable(wordcloud) | |
output$plot <- renderPlot({ | |
v <- terms() | |
wordcloud_rep(names(v), v, scale=c(4,0.5), | |
min.freq = input$freq, max.words=input$max, | |
colors=brewer.pal(8, "Dark2")) | |
}) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(shinyIncubator) | |
shinyUI(fluidPage( | |
progressInit(), | |
# Application title | |
headerPanel("Word Cloud"), | |
# Sidebar with a slider and selection inputs | |
sidebarPanel(width = 5, | |
selectInput("selection", "Choose a book:", | |
choices = books), | |
actionButton("update", "Change"), | |
hr(), | |
sliderInput("freq", | |
"Minimum Frequency:", | |
min = 1, max = 50, value = 15), | |
sliderInput("max", | |
"Maximum Number of Words:", | |
min = 1, max = 300, value = 100) | |
), | |
# Show Word Cloud | |
mainPanel( | |
plotOutput("plot") | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment