-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
238 lines (185 loc) · 6.47 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
library(shiny)
library(markdown)
library(DT)
library(ggplot2)
library(dplyr)
library(plotly)
library(shinythemes)
happiness2015 <- read.csv("results/happiness_2015_clean.csv", stringsAsFactors = TRUE)
happiness2016 <- read.csv("results/happiness_2016_clean.csv", stringsAsFactors = TRUE)
happiness2017 <- read.csv("results/happiness_2017_clean.csv", stringsAsFactors = TRUE)
countries_all <- read.csv("results/countries_aggregated.csv", stringsAsFactors = TRUE)
# Define UI for application that draws a histogram
ui <- navbarPage(
theme = shinytheme("journal"),
title = "World Happiness",
tabPanel("Plot",
sidebarPanel(width = 3,
br(),
span("This app will help you explore the data behind the ",
tags$a("World Happiness Report.", href = "http://worldhappiness.report")),
br(), hr(),
helpText("Select year"),
actionButton("button2015", "2015"),
actionButton("button2016", "2016"),
actionButton("button2017", "2017"),
br(), br(),
helpText("Choose variables you want to see on the plot"),
uiOutput("variable_1"),
uiOutput("variable_2"),
br(),
helpText("Click while pressing `command` in order to choose multiple countries"),
uiOutput("country"),
br(), hr(),
span("Data source:",
tags$a("Kaggle",
href = "https://www.kaggle.com/unsdsn/world-happiness")),
br(), br(),
em(
span("Created by", a(href = "https://nazliozum.github.io/", "Nazlı Özüm Kafaee")),
br(),
span("Code", a(href = "https://github.com/Nazliozum/world_happiness", "on GitHub"))
)),
mainPanel(
tabsetPanel(
tabPanel("Plot",
br(),
h2(textOutput("year_name_plot")),
plotlyOutput("scatterplot",
height = 800)
),
tabPanel("Table",
br(),
DT::dataTableOutput("results_table")
),
tabPanel(textOutput("tab_name_rankings"),
h1(textOutput("year_name")),
h2("Top 10"),
wellPanel(
dataTableOutput("rank_table_top")),
br(),
h2("Bottom 10"),
wellPanel(
dataTableOutput("rank_table_bottom"))
)
)
)),
tabPanel("About",
includeMarkdown("docs/About.md"))
)
##############################################
### SERVER STARTS HERE #######################
##############################################
server <- function(input, output, session) {
output$variable_1 <- renderUI({
selectInput("variable_1", "Y-variable:",
choices = c("Happiness.Rank", "Happiness.Score", "Economy",
"Family", "Health", "Freedom", "Government.Corruption",
"Generosity", "Dystopia.Residual"), selected = "Happiness.Score")
})
output$variable_2 <- renderUI({
selectInput("variable_2", "X-variable:",
choices = c("Happiness.Rank", "Happiness.Score", "Economy",
"Family", "Health", "Freedom", "Government.Corruption",
"Generosity", "Dystopia.Residual"), selected = "Economy")
})
data_year <- reactiveValues(data = happiness2015, year = "2015")
observeEvent(input$button2015, {
data_year$data <- happiness2015
year <- "2015"
})
observeEvent(input$button2016, {
data_year$data <- happiness2016
data_year$year <- "2016"
})
observeEvent(input$button2017, {
data_year$data <- happiness2017
data_year$year <- "2017"
})
filtered_data <- reactive({
data_year$data %>%
filter(Country %in% c(input$country)) %>%
arrange(Country)
})
output$year_name_plot <- renderText({
paste("Data for ", data_year$year)
})
countries <- reactive({
df_small <-
countries_all %>%
select(Country) %>%
droplevels()
c("Select All", levels(df_small$Country))
})
output$country <- renderUI ({
selectInput("country", "Countries:",
multiple = TRUE,
choices = countries(),
selected = "Select All",
selectize = FALSE,
size = 10)
})
observe({
if ("Select All" %in% input$country) {
# choose all the choices _except_ "Select All"
selected_choices <- setdiff(countries(), "Select All")
updateSelectInput(session, "country", selected = selected_choices)
}
})
output$scatterplot <- renderPlotly({
if (is.null(data_year$data)) return()
p <- ggplot(filtered_data()) +
geom_point(aes_string(x = input$variable_2, y = input$variable_1,
colour = "Region", label = "Country"), size = 3) +
ggtitle(paste0(input$variable_1, " vs. ", input$variable_2)) +
theme_bw() +
theme(legend.position = "bottom")
ggplotly(p, tooltip = c("label", "x", "y"), height = 500)
})
output$results_table <- DT::renderDataTable({
if (is.null(data_year$data)) {
return()
} else {
filtered_data()
}
},
options = list(lengthChange = FALSE,
scrollCollapse = TRUE,
scrollX = "100%"),
selection = "single"
)
output$tab_name_rankings <- renderText({
paste("Happiness Ranking: ", data_year$year)
})
output$year_name <- renderText({
paste("Happiness Ranking in ", data_year$year)
})
output$rank_table_top <- DT::renderDataTable({
if (is.null(data_year$data)) {
return()
} else {
data_year$data %>%
mutate(Rank = Happiness.Rank) %>%
arrange(Rank) %>%
top_n(10, desc(Rank))
}
},
options = list(lengthChange = FALSE,
scrollX = "100%")
)
output$rank_table_bottom <- DT::renderDataTable({
if (is.null(data_year$data)) {
return()
} else {
data_year$data %>%
mutate(Rank = Happiness.Rank) %>%
arrange(Rank) %>%
top_n(10, Rank)
}
},
options = list(lengthChange = FALSE,
scrollX = "100%")
)
}
# Run the application
shinyApp(ui = ui, server = server)