-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.R
158 lines (132 loc) · 5.78 KB
/
app.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
# version to get data from S3 bucket files
library(shiny)
library(tidyverse)
library(rtweet)
library(ggthemes)
library(aws.s3)
library(lubridate)
library(DT)
# setting DB connection and getting users and tweets
setwd("~/aupolitics")
# Use local copy of data
# load tweets from local file and compare by status_id with Db
load("tweets_app.RData")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Google Analytics code
tags$head(includeHTML("ga.html")),
# Application title
titlePanel("Australian Members of Parliament Twitter statistics. Updated daily"),
# Sidebar with a selector for party
sidebarLayout(
sidebarPanel(
selectInput("party", "Select party(s), use 'Backspace' key to delete",
c("Australian Labor Party", "Liberal Party of Australia", "The Nationals"),
selected=c("Australian Labor Party", "Liberal Party of Australia", "The Nationals"),
selectize = TRUE, multiple = TRUE),
dateInput("start_date", "Start date:", value=today()-30, min=today()-90, max=today()-1)
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Devices",plotOutput("devPlot")),
tabPanel("Timeline", plotOutput("timePlot")),
tabPanel("Top tweets",
# Scatterplot
plotOutput(outputId = "tweetsPlot", brush = "plot_brush"),
tags$br(),
# Show data table
dataTableOutput(outputId = "tweetsTable")
),
tabPanel("Top tweets table", DT::dataTableOutput("toptweetsTable"))
)
)
),
# Footer notes
hr(),
div(class = "footer",
includeHTML("footer.html")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
# Cross-session reactive file reader.
fileData <- reactiveFileReader(1000, NULL, 'tweets_app.RData', load)
## End(Not run)
# reactive function to update data frame based on partie(s) and date
t1 <- reactive({
req(input$party, input$start_date) # ensure input$ vars available
filter(t, as.Date(created_at)>as.Date(input$start_date)) %>% filter(party %in% input$party)
})
#tab 1 plot
output$devPlot <- renderPlot({
# generate col chart for devices used per party based on input$party from ui.R
clients <- t1() %>% group_by(source=as.factor(source)) %>%
summarise(count = n()) %>%
top_n(5, count) %>% arrange(desc(count))
# draw the col chart
clients %>% ggplot(aes(reorder(source,count),count, fill=source, label = count))+
geom_col()+
labs(x="", y="Number of tweets", fill="Source", title = "Twitter sources/devices")+
geom_text(size = 3, position = position_stack(vjust = 0.5))+
theme_economist()
})
output$timePlot <- renderPlot({
# status updates by party since date selected
ts_plot(t1(), by = "1 day", trim=1, tz="Australia/Sydney") +
ggplot2::theme_light() +
ggplot2::theme(plot.title = ggplot2::element_text(face = "bold")) +
ggplot2::labs(
x = NULL, y = NULL,
title = paste0("Frequency of selected party MPs tweets, since ", as.character(input$start_date)),
subtitle = "Tweets counts aggregated using 1-day intervals, trimmed",
caption = "\nSource: Data collected from Twitter's REST API via rtweet"
)+
theme_economist()
})
output$tweetsPlot <- renderPlot({
# get likes vs retweets, original only
t2 <- t1() %>% ungroup() %>%
filter (is.na(retweet_status_id)) %>% plain_tweets() %>%
mutate (link=paste0('<a href=', status_url, ' target="_blank" >On Twitter</a>')) %>%
select (text, screen_name, retweets=retweet_count, likes=favorite_count, link, party)
# draw scatterplot
ggplot(t2, aes_string(x = t2$likes, y = t2$retweets)) +
geom_point(aes(color=party), size=3)+
ggplot2::theme_light() +
ggplot2::theme(plot.title = ggplot2::element_text(face = "bold")) +
scale_x_log10()+
scale_y_log10()+
ggplot2::labs(
x = 'Likes', y = 'Retweets',
title = "Likes vs retweets",
caption = "\nSource: Data collected from Twitter's REST API via rtweet"
)+
theme_economist()
})
# Create data table
output$tweetsTable <- renderDataTable({
t2 <- t1() %>% ungroup() %>%
filter (is.na(retweet_status_id)) %>% plain_tweets() %>%
mutate (link=paste0('<a href=', status_url, ' target="_blank" >On Twitter</a>')) %>%
select (text, screen_name, retweets=retweet_count, likes=favorite_count, link, party)
b <- brushedPoints(t2, brush = input$plot_brush, xvar="likes", yvar="retweets") %>%
select(text, screen_name, party, likes, retweets, link)
print (b)
})
output$toptweetsTable <- DT::renderDataTable({
# find top 200 tweets for the party over given period of time
t2 <- t1() %>% ungroup() %>%
filter (is.na(retweet_status_id)) %>% plain_tweets() %>%
mutate (link=paste0('<a href=', status_url, ' target="_blank" >On Twitter</a>')) %>%
select (text, screen_name, retweets=retweet_count, likes=favorite_count, link, party) %>%
top_n (200, likes+retweets) %>% arrange(likes)
DT::datatable(t2, escape = FALSE,
options = list(order = list(list(4, 'desc'), list(3, 'desc')))
)
})
}
# Run the application
shinyApp(ui = ui, server = server)