-
Notifications
You must be signed in to change notification settings - Fork 2
/
HDRnotebook.Rmd
208 lines (173 loc) · 5.77 KB
/
HDRnotebook.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
---
title: "HDRnotebook"
author: "GENG Minghong"
date: "Last Updated: `r Sys.Date()`"
output: html_document
---
# 1. Load Data and Data Preparation
The data of this project can be accessed here:
http://hdr.undp.org/en/data
Done beofre 4.17
```{r Introduction, eval=TRUE, echo=TRUE, message=FALSE, warning=FALSE}
# echo : the result
# eval : run or not. Remember if here you set it as false, the code here won't run. So don't put `library( )` here
# fig.height is about the graph of this chunk.
packages = c('corrplot','ggpubr','plotly','tidyverse','readxl')
for(p in packages){library
if (!require(p,character.only = T)){
install.packages(p)
}
library(p,character.only = T)
}
```
```{r}
data1=read_excel('data/Table1.HDI&Components.xlsx')
```
# 2. Exploratory Data Analysis
# 3. Data Visulisation Design
some thoughts here.
# 4. shiny
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Install Packages
```{r}
packages = c('tidyverse','reshape','ggplot2','tidyr','gganimate','stringr','plotly','RColorBrewer','adegenet','NAM','snow','doSNOW','parallel','circular','d3heatmap','plotly','viridis','hrbrthemes','grid','gtable','leaflet')
for (p in packages){
if(!require(p,character.only = T)){
install.packages(p)
}
library(p, character.only = T)
}
install.packages('leaflet')
library(leaflet)
library(shiny)
```
### Read File
```{r}
# Prepare data
all <- read.csv('data/All.csv', header = TRUE)
names(all)[4]="Country"
names(all)[1]='Continent'
choice <- colnames(all)[1:4]
head(all)
print(choice)
```
### Shiny
```{r}
#### User Interface
ui <- bootstrapPage(
#shinythemes::themeSelector(),
tags$head("Human Development Report"),
navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
"Human Development Report", id="nav",
tabPanel("World mapper",
div(leafletOutput("mymap"),
p(),
actionButton("recalc", "New points")
)),
tabPanel("HDI"),
tabPanel("Gender Development Index"),
tabPanel("Poverty Index"),
tabPanel("Population"),
tabPanel("Health"),
tabPanel("Education"),
tabPanel("Data"),
tabPanel("About us")),
sidebarLayout(
sidebarPanel(width = 3,
selectInput('level','Choose a Level', choices = choice),
selectInput("country","Choose countries",choices = unique(HDI$Country), multiple = TRUE),
sliderInput("year",'choose year'锛? min = 1990, max = 2018, value = c(1990,2018),step = 1),
actionButton("Search", "Search"),
actionButton("Help","About")
),
mainPanel(
fluidRow(column(plotly::plotlyOutput(outputId = "LEtrend"),width = 4, height = 3),
column(plotly::plotlyOutput(outputId = "MStrend"),width = 4),
column(plotly::plotlyOutput(outputId = "EStrend"),width = 4)),
fluidRow(column(plotly::plotlyOutput(outputId = "GNItrend"),width = 4),
column(plotly::plotlyOutput(outputId = "HDItrend"),width = 4))
)
)
)
#### Server
server<-function(input, output, session){
## filter data
extract_data <- reactive({
all %>%
filter(Country == input$country,
Year >= input$year[1],
Year <= input$year[2])
})
## HDI trend plot
reactive_HDI <- eventReactive(input$Search,{
extract_data()%>%
plot_ly(x = ~Year, y=~HDI, color = ~Country, hoverinfo = "text",
text = ~paste(input$country, HDI)) %>%
add_lines()%>%
layout(showlegend=TRUE)
})
output$HDItrend <- renderPlotly({reactive_HDI()})
## Life Expectancy trend plot
reactive_LifeExpectancy <- eventReactive(input$Search,{
extract_data()%>%
plot_ly(x = ~Year, y=~Life_Expectancy, color = ~Country, hoverinfo = "text",
text = ~paste(input$country, Life_Expectancy)) %>%
add_lines()%>%
layout(showlegend=TRUE)
})
output$LEtrend <- renderPlotly({reactive_LifeExpectancy()})
## Expected Schooling trend plot
reactive_ExpectedSchooling <- eventReactive(input$Search,{
extract_data()%>%
plot_ly(x = ~Year, y=~Expected_Years_of_Schooling, color = ~Country, hoverinfo = "text",
text = ~paste(input$country, Expected_Years_of_Schooling)) %>%
add_lines()%>%
layout(showlegend=TRUE)
})
output$EStrend <- renderPlotly({reactive_ExpectedSchooling()})
## Mean Schooling trend plot
reactive_MeanSchooling <- eventReactive(input$Search,{
extract_data()%>%
plot_ly(x = ~Year, y=~Mean_Years_of_Schooling, color = ~Country, hoverinfo = "text",
text = ~paste(input$country, Mean_Years_of_Schooling)) %>%
add_lines()%>%
layout(showlegend=TRUE)
})
output$MStrend <- renderPlotly({reactive_MeanSchooling()})
## GNI per capita trend plot
reactive_GNI <- eventReactive(input$Search,{
extract_data()%>%
plot_ly(x = ~Year, y=~GNI_per_capita, color = ~Country, hoverinfo = "text",
text = ~paste(input$country, GNI_per_capita)) %>%
add_lines()%>%
layout(showlegend=TRUE)
})
output$GNItrend <- renderPlotly({reactive_GNI()})
}
shinyApp(ui = ui, server = server)
```
```{r}
output$duration_table <- renderTable({
HDI %>%
filter(
Country == input$country,
Year >= input$year[1],
Year <= input$year[2]
) %>%
group_by(shape) %>%
summarize(
nb_sighted = n(),
avg_duration = mean(duration_sec),
median_duration = median(duration_sec),
min_duration = min(duration_sec),
max_duration = max(duration_sec)
)
})
```
```{r}
install.packages("shinyWidgets")
library(shinyWidgets)
shinyWidgetsGallery()
```