-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shiny for Final.R
102 lines (82 loc) · 2.29 KB
/
Shiny for Final.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
library(shiny)
library(tidyverse)
library(readr)
library(dplyr)
library(ggplot2)
library(twitteR)
library(httpuv)
library(scales)
library(tm)
library(stringr)
library(wordcloud)
library(knitr)
library(tidytext)
library(syuzhet)
library(lubridate)
library(scales)
library(reshape2)
library(dplyr )
library(rtweet)
library(feather)
library(RColorBrewer)
tweetswords <-read_csv("tweetswords.csv")
tweetswords %>%
head() %>%
knitr::kable(caption = "Tweets with the word immigration")
reg <- "([^A-Za-z\\d#@']|'(?![A-Za-z\\d#@]))"
tweet_words <-tweetswords %>%
filter(!str_detect(text, '^"')) %>%
mutate(text = str_replace_all(text, "https://t.co/[A-Za-z\\d]+|&", "")) %>%
unnest_tokens(word, text, token = "regex", pattern = reg) %>%
filter(!word %in% stop_words$word,
str_detect(word, "[a-z]"))
tweet_dates2 <- tweet_words %>%
separate(created, into = c("date", "time"), sep = -9)
feather::write_feather(tweet_dates2, "tweet_dates2.feather")
tweet_dates2 <- feather::read_feather("tweet_dates2.feather")
#User Interface
ui <- fluidPage(
titlePanel("Tweeting Immigration"),
sidebarLayout(
sidebarPanel(
selectizeInput(
"word",
"Words",
choices = sort(unique(tweet_words$word)),
multiple = TRUE
)
),
mainPanel(
plotOutput("wordcloud"),
plotOutput("sentiment"))
))
#Server Code
server <- shinyServer(function(input, output) {
#Filter Tweets
filtered_tweets <- reactive({
if(is.null(input$word)) {
return(NULL)
}
#Function to take term input, filter, and inner join with other words
tweet_words %>%
filter(word %in% input$word) %>%
select(id) %>%
unique() %>%
inner_join(tweet_words)
})
output$wordcloud <- renderPlot({
if(is.null(filtered_tweets())){
return()
}
tweet_words_count <- filtered_tweets() %>%
count(word, sort = TRUE) %>%
arrange(desc(n)) %>%
filter(word != "immigration",
word != "immigrant",
word != "#immigration",
word != "immigrants")
wordcloud(words = tweet_words_count$word, freq = tweet_words_count$n, scale=c(8,.3),
min.freq = 500, random.order = FALSE, rot.per=.15, colors = brewer.pal(8,"Dark2"))
})
})
shinyApp(ui = ui, server = server)