-
Notifications
You must be signed in to change notification settings - Fork 0
/
FP.R
111 lines (95 loc) · 2.55 KB
/
FP.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
102
103
104
105
106
107
108
109
#Part 2: UI
library(shiny)
library(tidyverse)
library(rsconnect)
# get text
Livy <- read_csv("All_Livy.csv")%>%
mutate(Book = parse_number(Book))
# convert to tokens
Livy <- Livy %>%
group_by(Book, Chapter) %>%
unnest_tokens(word, Text) %>%
mutate(linenumber = row_number())
# remove stop words
cleaned_livy <- Livy %>%
anti_join(stop_words)
cleaned_livy %>%
count(word, sort = TRUE)
# add sentiment from NRC dictionary
LivySentiment <- cleaned_livy %>%
inner_join(get_sentiments("nrc"))
# summarize sentiment by chapter
LivySentimentCount <- LivySentiment %>%
count(Book, Chapter, sentiment) %>%
left_join(cleaned_livy %>%
count(Book, Chapter) %>%
rename(n_total = n)) %>%
mutate(pct = n / n_total)
chapter_id <- Livy %>%
select(Book, Chapter) %>%
distinct() %>%
ungroup %>%
mutate(livy_id = row_number())
LivySentimentCount <- LivySentimentCount %>%
left_join(chapter_id) %>%
arrange(livy_id, sentiment)
break_points <- chapter_id %>%
group_by(Book) %>%
slice(1) %>%
na.omit()
shinyUI(
fluidPage(
titlePanel("Sentiments in Livy"),
sidebarLayout(
sidebarPanel(
sliderInput(
"Book",
"Book",
min = 0,
max = 35,
value = c(0, 35)
),
checkboxGroupInput(
"typeInput",
"Sentiments",
choices = tolower(c("Anger", "Anticipation", "Disgust", "Fear",
"Joy", "Sadness", "Surprise", "Trust", "Negative",
"Positive")),
selected = tolower(c("Anger", "Anticipation", "Disgust", "Fear",
"Joy", "Sadness", "Surprise", "Trust", "Negative",
"Positive"))
)
),
mainPanel(plotOutput("coolplot"),
tableOutput("results"))
)
)
)
server <- function(input, output) {
filtered <- reactive({
if(is.null(input$typeInput)) {
return(NULL)
}
LivySentimentCount %>%
filter(
Book >= input$Book[1],
Book <= input$Book[2],
sentiment %in% input$typeInput
)
})
observe({ print(filtered())})
output$coolplot <- renderPlot({
if (is.null(filtered())) {
return()
}
ggplot(filtered(), aes(livy_id, pct, group = sentiment,
color = sentiment)) +
geom_smooth(se = FALSE)+
ggtitle("Sentiments In Livy")+
xlab("Book and Chapter")+
ylab("Percent of Text per Chapter")+
scale_x_continuous(breaks = break_points$livy_id,
labels = break_points$Book)
})
}
shinyApp(ui = ui, server = server)