Created
August 1, 2018 11:35
-
-
Save InsiderPhD/f1a871b07fb515fc3e0eb38a47c564a1 to your computer and use it in GitHub Desktop.
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
# | |
# This is a Shiny web application. You can run the application by clicking | |
# the 'Run App' button above. | |
# | |
# Find out more about building applications with Shiny here: | |
# | |
# http://shiny.rstudio.com/ | |
# | |
library(dplyr) | |
library(stringr) | |
library(tm) | |
library(topicmodels) | |
library(tidytext) | |
library(RColorBrewer) | |
library(wordcloud) | |
# Define UI for application that draws a histogram | |
ui <- fluidPage( | |
# Application title | |
titlePanel("Word cloud for discord"), | |
# Sidebar with a slider input for number of bins | |
sidebarLayout( | |
sidebarPanel( | |
textInput("text", "Username (with tag):", "timtamtom#1612"), | |
numericInput("words", "Max. Words", "200"), | |
numericInput("pings", "Max. Pings", "20"), | |
submitButton("Submit") | |
), | |
# Show a plot of the generated distribution | |
mainPanel( | |
verbatimTextOutput("text"), | |
plotOutput("distPlot"), | |
plotOutput("pingsPlot") | |
) | |
) | |
) | |
# Define server logic required to draw a histogram | |
server <- function(input, output) { | |
discord <- readRDS("./data/discord.rds") | |
output$distPlot <- renderPlot({ | |
withProgress(message = 'Making Text Plot', value = 0, { | |
# generate bins based on input$bins from ui.R | |
incProgress(1/4, detail = paste("Loading user data 1/", 5)) | |
tom <- subset(discord, Author==input$text) | |
incProgress(1/5, detail = paste("Splitting messages into words 2/", 5)) | |
tom_words <- tom %>% | |
unnest_tokens(word, Content) | |
incProgress(1/5, detail = paste("Calculating frequencies 3/", 5)) | |
test<- as.data.frame(table(tom_words$word)) | |
incProgress(1/5, detail = paste("Removing Stop Words 4/", 5)) | |
stop_words <- get_stopwords() | |
test <- test %>% | |
anti_join(stop_words, by = c("Var1" = "word")) | |
l_etters <- as.data.frame(as.character(letters[seq( from = 1, to = 26 )])) | |
numbers <- data.frame(as.numeric(seq( from = 0, to = 9))) | |
colnames(l_etters) <- c("letters") | |
colnames(numbers) <- c("numbers") | |
l_etters$letters <- as.character(l_etters$letters) | |
numbers$numbers <- as.character(numbers$numbers) | |
test <- test %>% | |
anti_join(l_etters, by = c("Var1" = "letters")) | |
test <- test %>% | |
anti_join(numbers, by = c("Var1" = "numbers")) | |
incProgress(1/5, detail = paste("Creating Word Cloud 5/", 5)) | |
wordcloud(words = test$Var1, freq = test$Freq, min.freq = 1, | |
max.words=input$words, random.order=FALSE, rot.per=0.35, | |
colors=brewer.pal(8, "Dark2")) | |
}) | |
}) | |
output$pingsPlot <- renderPlot({ | |
withProgress(message = 'Making Ping Plot', value = 0, { | |
# generate bins based on input$bins from ui.R | |
incProgress(1/4, detail = paste("Loading user data 1/", 5)) | |
tom <- subset(discord, Author==input$text) | |
incProgress(1/5, detail = paste("Splitting messages into words 2/", 5)) | |
tom_words <- tom %>% | |
unnest_tokens(word, Content) | |
incProgress(1/5, detail = paste("Calculating frequencies 3/", 5)) | |
test<- as.data.frame(table(tom_words$word)) | |
incProgress(1/5, detail = paste("Isolating Pings 4/", 5)) | |
test <- test %>% | |
filter(str_detect(Var1, '[0-9]{4}')) | |
incProgress(1/5, detail = paste("Creating Word Cloud 5/", 5)) | |
wordcloud(words = test$Var1, freq = test$Freq, min.freq = 1, | |
max.words=input$pings, random.order=FALSE, rot.per=0.35, | |
colors=brewer.pal(8, "Dark2")) | |
}) | |
}) | |
output$text <- renderText({ | |
paste("Input: ", input$text) | |
}) | |
} | |
# Run the application | |
shinyApp(ui = ui, server = server) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment