-
Notifications
You must be signed in to change notification settings - Fork 2
/
github export app.Rmd
185 lines (164 loc) · 4.77 KB
/
github export app.Rmd
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
title: Shiny Application Using Twitter Data
author: Nipunjeet Gujral
data: May 27th, 2018
output:
html_document:
theme: spacelab
highlight: neon
---
```{r libraries, echo=FALSE, message=FASLE, warning=FALSE}
# library to attach to twitter API
library(twitteR)
# library to manipulate data
library(dplyr)
library(lubridate)
library(tidytext)
library(stringr)
library(tm)
# library to visualize data
library(wordcloud)
library(RColorBrewer)
library(ggplot2)
library(plotly)
# shiny related
library(shiny)
library(shinydashboard)
library(DT)
library(crosstalk)
```
```{r authentication}
# authrenticating twitter application credentials
authentication()
```
```{r UI}
ui <- dashboardPage(
# title ####
dashboardHeader(title = "Twitter Use Analysis"),
# menu ####
dashboardSidebar(
sidebarMenu(
menuItem(
"Charts",
tabName = "charts",
icon = icon("stats", lib = "glyphicon")
),
menuItem(
"Table",
tabName = "table",
icon = icon("th-large", lib = "glyphicon")
),
# text input ####
textInput(
inputId = "twitter_handle",
label = "Enter a Twitter Handle",
width = 300,
value = ""
),
# slider input ####
sliderInput(
inputId = "tweet_num",
label = "Numebr of Tweets",
min = 0, max = 1000,
value = 500
),
# action button ####
actionButton(
inputId = "click",
label = "submit"
)
)
),
# body ####
dashboardBody(
tabItems(
tabItem(
tabName = "charts",
fluidPage(
box(plotlyOutput(outputId = "tweets_per_day")),
box(plotlyOutput(outputId = "tweets_per_hour")),
box(plotlyOutput(outputId = "ratio_vs_time")),
box(plotlyOutput(outputId = "fav_vs_retweet")),
box(plotlyOutput(outputId = "metric_histograms")),
box(plotlyOutput(outputId = "tweet_source"))
)
),
tabItem(
tabName = "table",
h2("Plotly Linked to Original Data Table", align = "center"),
plotlyOutput(outputId = "x2"),
DT::dataTableOutput(outputId = "x1")
)
)
)
)
```
```{r server}
server <- function(input, output, ...){
observeEvent(input$click, {
# definning user handle and sample size ####
handle <- isolate({as.character(input$twitter_handle)})
sample_size <- isolate({input$tweet_num})
# pulling tweets ####
temp_tweets <- reactive({
pull_tweets(handle, sample_size)
})
# analysing tweets and time ####
output$tweets_per_day <- renderPlotly({
tweets_per_day(temp_tweets())
})
output$tweets_per_hour <- renderPlotly({
tweets_per_hour(temp_tweets())
})
output$ratio_vs_time <- renderPlotly({
ratio_vs_time(temp_tweets())
})
# variable distribution ####
output$fav_vs_retweet <- renderPlotly({
fav_vs_retweet(temp_tweets())
})
output$metric_histograms <- renderPlotly({
distributions(temp_tweets())
})
# tweet source ####
output$tweet_source <- renderPlotly({
tweet_source(temp_tweets())
})
# table ####
m <- temp_tweets() %>%
tibble::rownames_to_column()
d <- SharedData$new(m, ~rowname)
output$x2 <- renderPlotly({
s <- input$x1_rows_selected
if (!length(s)) {
p <- d %>%
plot_ly(x = ~index, y = ~ratio, mode = "markers", color = I('steelblue'), name = 'Unfiltered') %>%
layout(showlegend = TRUE) %>%
highlight("plotly_selected", color = I('red'), selected = attrs_selected(name = 'Filtered'))
} else if (length(s)) {
pp <- m %>%
plot_ly() %>%
add_trace(x = ~index, y = ~ratio, mode = "markers", color = I('steelblue'), name = 'Unfiltered') %>%
layout(showlegend = TRUE)
# selected data
pp <- add_trace(pp, data = m[s, , drop = FALSE], x = ~index, y = ~ratio, mode = "markers",
color = I('red'), marker = list(size = 10), name = 'Filtered')
}
})
output$x1 <- DT::renderDataTable({
m2 <- m[d$selection(),]
dt <- DT::datatable(m)
if (NROW(m2) == 0) {
dt
} else {
DT::formatStyle(dt, "rowname", target = "row",
color = DT::styleEqual(m2$rowname, rep("white", length(m2$rowname))),
backgroundColor = DT::styleEqual(m2$rowname, rep("blue", length(m2$rowname))))
}
})
})
}
```
```{r initiate application}
shinyApp(ui = ui, server = server)
```