This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sentiment_timeline_module.R
126 lines (109 loc) · 4.08 KB
/
sentiment_timeline_module.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
sentimentTimelineUI <- function(id){
ns <- NS(id)
tagList(
description = div(
"This chart shows the average sentiment of matching search results for each month, by data set."
),
plot = div(
style = "max-width: 1200px;",
fluidRow(
column(
offset = 8, # this puts the switch over to the right
width = 3,
shinyWidgets::materialSwitch(inputId = ns("show_trend"),
value = FALSE,
label = "Show Trend Line?",
status = "primary",
right = TRUE)
)
),
shiny::uiOutput(ns("sentiment_timeline_plot_ui")) %>%
shinycssloaders::withSpinner(id = ns("this spinner"))
)
)
}
sentimentTimeline <- function(input, output, session,
data_set_info,
query_info) {
plot_data <- reactive({
req(query_info()$num_hits > 0)
aggregations = query_text_depot(query_info = query_info(),
aggregates_json = sentimentTimelineQuery())
aggregations = parse_aggregates(es_results = aggregations)
plot_hits = aggregations$month_counts.buckets %>%
transmute(Date = as.Date(stringr::str_sub(key_as_string, 1, 10)), Sentiment = sentiment.value, index_name = index, Count = doc_count) %>%
left_join(select(data_set_info(), index_name, display_name), by = "index_name") %>% # to get display name
mutate(Count = ifelse(Count == 0, NA, Count)) %>%
arrange(Date) %>%
filter(!is.na(Sentiment)) %>%
arrange(display_name)
return(plot_hits)
})
output$sentiment_timeline_plotly <- plotly::renderPlotly({
req(plot_data)
p <- plot_data() %>%
plot_timeseries_td(
date_var = Date,
value_var = Sentiment,
group_var = display_name,
colour_var = display_name,
data_set_info = data_set_info(),
scales = "free_y",
show_trend = input$show_trend,
date_format = "%Y-%b"
)
# https://stackoverflow.com/questions/44569551/date-format-in-hover-for-ggplot2-and-plotly
p <- p +
geom_line(aes(text = paste0("<b>", display_name, '</b>\n',
"Average Sentiment: ", sigfig(Sentiment, n = 2), '\n',
"Document Count: ", Count, '\n',
"Date: ", format(Date, format = "%b-%Y")))) %>%
suppressWarnings()
not_enough_data <- plot_data() %>%
group_by(display_name) %>%
filter(n() <= 1)
if (nrow(not_enough_data) > 0) {
p <- p + geom_point(data = not_enough_data, mapping = aes(text = paste0("<b>", display_name, '</b>\n',
"Average Sentiment: ", sigfig(Sentiment, n = 2), '\n',
"Document Count: ", Count, '\n',
"Date: ", format(Date, format = "%b-%Y")))) %>%
suppressWarnings()
}
p1 <- plotly::ggplotly(p, tooltip = c("text"), dynamicTicks = TRUE) %>%
layout(margin = list(l = 75, r = 75))
for (x in names(p1$x$layout)[grepl("yaxis", names(p1$x$layout))]) {
p1[["x"]][["layout"]][[x]][["fixedrange"]] <- TRUE
}
return(p1)
})
output$sentiment_timeline_plot_ui <- renderUI({
n_facets <- n_distinct(plot_data()$display_name)
plotly::plotlyOutput(session$ns("sentiment_timeline_plotly"), height = 200 + (100 * n_facets))
})
}
sentimentTimelineQuery <- function() {
'
"aggs": {
"group_by_index": {
"terms": {
"field": "_index"
},
"aggs" : {
"month_counts": {
"date_histogram" : {
"field" : "date",
"calendar_interval" : "month"
},
"aggs" : {
"sentiment" : {
"avg" : {
"field" : "sentiment_polarity"
}
}
}
}
}
}
}
'
}