This is an R Markdown document for my coding of shiny and flexdashboard. For complete product, please visit the shiny-io site. < https://wo4yq3-stanley-cheng.shinyapps.io/Global_Data_Dashboard/>.
knitr::opts_chunk$set(eval = FALSE, include = FALSE)
library(flexdashboard)
library(tidyverse)
library(ggthemes)
library(shiny)
library(plotly)
library(scales)
library(leaflet)
library(maps)
library(rio)
library(rsconnect)
library(shinythemes)
data=import("Data.csv", header=T, na.strings=c("..","NA"))
names(data)=c("series","series_code","country","country_code",2000:2015)
## Population
pop=data %>% filter(series=="Population, total") %>% drop_na()%>% select(-c("series","series_code","country_code"))
## Population Growth
pop_growth=data %>% filter(series=="Population growth (annual %)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
## Surface Area
area=data %>% filter(series=="Surface area (sq. km)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
## GDP
gdp=data %>% filter(series=="GDP (current US$)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
## GDP Growth
gdp_growth=data %>% filter(series=="GDP growth (annual %)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
## GDP per capita
gdp_per_capita=data %>% filter(series=="GDP per capita (current US$)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
## Inflation Rate (GDP Deflator)
gdp_inflation=data %>% filter(series=="Inflation, GDP deflator (annual %)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
country_list=Reduce(intersect,list(pop[1],pop_growth[1],area[1],gdp[1],gdp_growth[1],gdp_per_capita[1],gdp_inflation[1]))
gdp_agriculture=data %>% filter(series=="Agriculture, forestry, and fishing, value added (% of GDP)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
gdp_industrial=data %>% filter(series=="Industry (including construction), value added (% of GDP)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
gdp_ex=data %>% filter(series=="Exports of goods and services (% of GDP)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
gdp_im=data %>% filter(series=="Imports of goods and services (% of GDP)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
gdp_merchandise=gdp_im=data %>% filter(series=="Merchandise trade (% of GDP)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
country_list=Reduce(intersect,list(country_list,gdp_agriculture[1],gdp_industrial[1],gdp_ex[1],gdp_im[1],gdp_merchandise[1]))
## import the world.cities data frame from 'maps' and match name with our country list
data(world.cities)
world_city=data.frame(country=list(world.cities$country.etc))
names(world_city)[1]="country"
world_city=unique(world_city)
country_list=Reduce(intersect,list(country_list, world_city) )
## Life Expectancy
life_expect=data %>% filter(series=="Life expectancy at birth, total (years)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
life_expect_average=data.frame(transpose(data.frame(colMeans(life_expect[2:17]))))
names(life_expect_average)=(c(2000:2015))
life_expect_average$country="Average"
life_expect_all=rbind(life_expect,life_expect_average)
life_expect_all_longer=life_expect_all %>% pivot_longer(-country,names_to ="year", values_to="life_expectancy")
mean_2015_life=(life_expect_all_longer %>% filter(country=="Average" & year==2015) %>% select(life_expectancy))[1]
## Fertility
fertility=data %>% filter(series=="Fertility rate, total (births per woman)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
fertility_average=data.frame(transpose(data.frame(colMeans(fertility[2:17]))))
names(fertility_average)=(c(2000:2015))
fertility_average$country="Average"
fertility_all=rbind(fertility,fertility_average)
fertility_all_longer=fertility_all %>% pivot_longer(-country,names_to ="year", values_to="fertility")
## Mortality
mortality_under5=data %>% filter(series=="Mortality rate, under-5 (per 1,000 live births)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
mortality_under5_average=data.frame(transpose(data.frame(colMeans(mortality_under5[2:17]))))
names(mortality_under5_average)=(c(2000:2015))
mortality_under5_average$country="Average"
mortality_under5_all=rbind(mortality_under5,mortality_under5_average)
mortality_under5_all_longer=mortality_under5_all %>% pivot_longer(-country,names_to ="year", values_to="mortality_under5")
## Adolescent Fertility
adolescent_fert=data %>% filter(series=="Adolescent fertility rate (births per 1,000 women ages 15-19)") %>% drop_na()%>% select(-c("series","series_code","country_code"))
adolescent_fert_all_longer=adolescent_fert %>% pivot_longer(-country,names_to ="year", values_to="adolescent_fert")
country_list=Reduce(intersect,list(country_list,life_expect[1],fertility[1],mortality_under5[1],adolescent_fert[1]))
knitr::include_graphics(“logo2-100height.png”)
Column {data-width=8}
------------
### Altas
# find the coordinate of capital and cities of selected country
# find the capital
selected_country_capital=reactive ({
world.cities %>%
filter(capital == 1 & country.etc==input$country_1) %>%
select(name=name, country=country.etc, lat=lat, lng = long)
})
# find the cities with population > 100000
selected_country_cities= reactive ({
world.cities %>%
filter(capital != 1 & country.etc==input$country_1) %>%
select(name=name, country=country.etc, lat=lat, lng = long, population=pop)%>%
arrange(desc(population)) %>% filter(population>=100000)
})
capital_lng=reactive({ selected_country_capital()$lng })
capital_lat=reactive({ selected_country_capital()$lat })
capital_name=reactive({ selected_country_capital()$name })
cities_lng=reactive({ selected_country_cities()$lng })
cities_lat=reactive({ selected_country_cities()$lat })
cities_name=reactive({ selected_country_cities()$name })
renderLeaflet(
if(nrow(selected_country_cities())!=0){
leaflet(world.cities)%>%
setView(lng=capital_lng(),lat=capital_lat(),zoom=6) %>%
addTiles()%>%
addPopups(lng=capital_lng(),lat=capital_lat(),popup=paste("Country: ",input$country_1,"<br>","Capital: ", capital_name()),options=popupOptions(closeButton=FALSE)) %>%
addCircleMarkers(lng=cities_lng(),lat=cities_lat(),label=cities_name(),radius=5,color="green",fillOpacity = 0.5) %>%
addCircleMarkers(lng=capital_lng(),lat=capital_lat(),radius=10,color="red",fillOpacity = 0.5)
} else {
leaflet(world.cities)%>%
setView(lng=capital_lng(),lat=capital_lat(),zoom=6) %>%
addTiles()%>%
addPopups(lng=capital_lng(),lat=capital_lat(),popup=paste("Country: ",input$country_1,"<br>","Capital: ", capital_name()),options=popupOptions(closeButton=FALSE)) %>% addCircleMarkers(lng=capital_lng(),lat=capital_lat(),radius=10,color="red",fillOpacity = 0.5)
}
)
renderPlot(
ggplot(data=map_data("world"),aes(x=long,y=lat,group=group))+geom_polygon(fill="mintcream",color="lightgrey")+
geom_polygon(data=map_data("world") %>% filter(region==input$country_1),aes(x=long,y=lat,group=group,fill=region),show.legend =FALSE)+
theme_void()
)
renderDataTable(
data.frame(world.cities) %>%
filter(country.etc==input$country_1 & (pop>=100000 | capital==1)) %>%
select(country=country.etc,city=name,lat=lat,lng=long,population=pop) , options=list(scrollY="200px"))
pop_2015=reactive({round(pop %>% filter(country==input$country_1) %>% select("2015")/1000000,3)})
renderValueBox( valueBox(prettyNum(pop_2015(),big.mark=","),caption="(Million) Population ", icon="fa-user-alt",color="skyblue")
)
area_2015=reactive({area %>% filter(country==input$country_1) %>% select("2015")})
renderValueBox( valueBox(prettyNum(area_2015(),big.mark=","),caption="Sq. Km of Area ", icon="fa-globe-americas",color="deepskyblue")
)
gdp_2015=reactive({round(gdp %>% filter(country==input$country_1) %>% select("2015")/1000000000,3)})
renderValueBox( valueBox(prettyNum(gdp_2015(),big.mark=","),caption="(Billion) USD in GDP", icon="fa-dollar-sign",color="dodgerblue")
)
gdp_per_capita_2015=reactive({round(gdp_per_capita %>% filter(country==input$country_1) %>% select("2015"),3)})
renderValueBox( valueBox(prettyNum(gdp_per_capita_2015(),big.mark=","),caption="USD in GDP per capita", icon="fa-coins",color="cornflowerblue")
)
pop_selected=reactive({pop %>% filter(country==input$country_1) %>% pivot_longer(-country,names_to ="year", values_to="year_value") })
renderPlotly( ggplotly(
ggplot(pop_selected())+
geom_line(stat="identity",aes(x=year,y=year_value,group=country),colour="royalblue",show.legend =FALSE)+
scale_y_continuous(name="Population",label=comma)+
labs(title=paste("Population from 2000 to 2015 in",input$country_1))+
theme_few() )
)
gdp_selected=reactive({gdp %>% filter(country==input$country_1) %>% pivot_longer(-country,names_to ="year", values_to="year_value") })
renderPlot(ggplot(gdp_selected())+
geom_bar(stat="identity",aes(x=year,y=year_value/1000000000),fill="Royalblue")+
scale_y_continuous(name="USD (in Billion)",label=comma)+
labs(title=paste("GDP from 2000 to 2015 in",input$country_1))+
theme_few()
)
gdp_per_capita_selected=reactive({gdp_per_capita %>% filter(country==input$country_1) %>% pivot_longer(-country,names_to ="year", values_to="year_value")})
renderPlotly ( ggplotly(
ggplot(gdp_per_capita_selected())+
geom_line(stat="identity",aes(x=year,y=year_value,group=country,color="blue"),show.legend =FALSE)+
geom_point(aes(x=year,y=year_value,color="green"),show.legend =FALSE)+
scale_y_continuous(name="USD",label=comma)+
labs(title=paste("GDP per capita from 2000 to 2015 in",input$country_1))+
theme_few()+ theme(legend.position='none') )
)
selected_gdp_agriculture=reactive({gdp_agriculture %>% filter(country==input$country_1) %>% pivot_longer(-country,names_to="year", values_to="gdp_agriculture") })
selected_gdp_industrial=reactive({gdp_industrial %>% filter(country==input$country_1) %>% pivot_longer(-country,names_to="year", values_to="gdp_industrial") })
selected_gdp_ex=reactive({gdp_ex %>% filter(country==input$country_1) %>% pivot_longer(-country,names_to="year", values_to="gdp_ex") })
selected_gdp_im=reactive({gdp_im %>% filter(country==input$country_1) %>% pivot_longer(-country,names_to="year", values_to="gdp_im") })
selected_gdp_composition= reactive({ cbind(selected_gdp_agriculture(),selected_gdp_industrial())%>% select("year","gdp_agriculture","gdp_industrial") })
renderPlotly(
ggplotly( ggplot(selected_gdp_composition() %>% pivot_longer(-year,names_to="sector",values_to="gdp_percent"))+
geom_bar(position="dodge",stat="identity",aes(x=year,y=gdp_percent,fill=sector))+
labs(title="GDP Percentage from Agricultural activities and Industrial activities")+
scale_fill_manual(values=c("Salmon","Tan"))+ylab("GDP Percentage")+theme_few() )
)
selected_gdp_ex=reactive({gdp_ex %>% filter(country==input$country_1) %>% pivot_longer(-country,names_to="year", values_to="gdp_ex") })
selected_gdp_im=reactive({gdp_im %>% filter(country==input$country_1) %>% pivot_longer(-country,names_to="year", values_to="gdp_im") })
selected_gdp_nx= reactive({ cbind(selected_gdp_ex(),selected_gdp_im())%>% select("year","gdp_ex","gdp_im") })
selected_gdp_nx_1= reactive({ selected_gdp_nx() %>% mutate(gdp_nx=gdp_ex-gdp_im) })
renderPlotly(
ggplotly(
ggplot(selected_gdp_nx_1(),aes(x=year,y=gdp_nx))+geom_point(group=1,color="brown")+geom_line(group=1,color="darkviolet")+ylab("Percentage of GDP")+
labs(title="GDP Percentage from Net Export (Total Export - Total Import) activities")+geom_hline(yintercept =0)+
theme_few() )
)
selected_gdp_merchandise=reactive({gdp_merchandise %>% filter(country==input$country_1) %>% pivot_longer(-country,names_to="year", values_to="gdp_merchandise") })
renderPlotly(
ggplotly(
ggplot(selected_gdp_merchandise(),aes(x=year,y=gdp_merchandise))+geom_point(group=1,color="gray")+geom_line(group=1,color="royalblue")+ylab("Percentage of GDP")+
labs(title="GDP Percentage from Merchandise Trade ")+
theme_few() )
)
Row {data-width=6}
------------------------
### Life Expectancy vs Fertility Rate
selected_life_expect=reactive({life_expect_all_longer %>% filter(country==input$country_1)})
selected_fertility=reactive({fertility_all_longer %>% filter(country==input$country_1)})
scatter_1=reactive({ merge(selected_life_expect(),selected_fertility()) })
renderPlotly( ggplotly(
ggplot(scatter_1())+geom_point(aes(x=life_expectancy,y=fertility),color="firebrick")+
geom_smooth(aes(x=life_expectancy,y=fertility),method=loess,formula=y~x,fill="lightpink",size=0.5,linetype=3)+
ylab("Fertility, Births Per Woman")+xlab("Life Expectancy, Years")+
labs("Relationship beween Life Expectancy and Fertility")+
theme_classic()
)
)
selected_adolescent_fert=reactive({adolescent_fert_all_longer %>% filter(country==input$country_1)})
selected_mortality_under5=reactive({mortality_under5_all_longer %>% filter(country==input$country_1)})
scatter_2=reactive({ merge(selected_adolescent_fert(),selected_mortality_under5()) })
renderPlotly( ggplotly(
ggplot(scatter_2())+geom_point(aes(x=adolescent_fert,y=mortality_under5),color="tomato")+
geom_smooth(aes(x=adolescent_fert,y=mortality_under5),method=loess,formula=y~x,fill="wheat",size=0.5,linetype=2)+
ylab("Mortality, deaths per 1000 child (< ages 5)")+
xlab("Adolescent Fertility, births per 1000 women (ages 15-19)")+
labs("Relationship beween Adolescent Fertility rate and Mortality rate")+
theme_classic()
)
)
life_expect_2015=reactive({as.numeric( round(life_expect %>% filter(country==input$country_1) %>% select("2015"),1)) })
renderGauge({
gauge(life_expect_2015(),min=0,max=100,symbol=" Yrs",gaugeSectors(success =c(80,100),warning=c(60,79),danger=c(0,59),color=c("green","orange","red") )
) })
fert_2015=reactive({as.numeric( round(fertility %>% filter(country==input$country_1) %>% select("2015"),1)) })
renderGauge({
gauge(fert_2015(),min=0,max=7.5,symbol=" births",gaugeSectors(success =c(0,2.5),warning=c(2.6,5),danger=c(5.1,7.5),color=c("green","orange","red") )
) })
mortality_under5_2015=reactive({as.numeric( round(mortality_under5 %>% filter(country==input$country_1) %>% select("2015"),1)) })
renderGauge({
gauge(mortality_under5_2015()/10,min=0,max=15,symbol=" %",gaugeSectors(success =c(0,2.5),warning=c(2.6,5),danger=c(6,15),color=c("green","orange","red") )
) })
adolescent_fert_2015=reactive({as.numeric( round(adolescent_fert %>% filter(country==input$country_1) %>% select("2015"),1))/10 })
renderGauge({
gauge(adolescent_fert_2015(),min=0,max=20,symbol=" %",gaugeSectors(success =c(0,2.5),warning=c(2.6,5),danger=c(6,20),color=c("green","orange","red") )
) })