Developing Data Products

SLamara
23.07.2018

GDP Vs HDI

To measure the progress of a nation, one could intuitively think about looking at its economic performance expressed by the Gross Domestic Production per capita (GDP per capita).

However, the United Nations Development Programme (UNPD) considers the Human Development index (HDI) as a better indicator as it includes different aspects of human development such as health, education and income.

ui.R

With my shiny app you can compare GDPs and corresponding HDIs from 2010 to 2015 and notice the strong and subtle relationship between these two indicators according to different levels of country'wealth.

library(shiny)
library(plotly)
shinyUI(fluidPage(
    titlePanel("Comparison between the GDPs per capita and corresponding Human Development Indices"),
    sidebarLayout(
        sidebarPanel(
            selectInput("selYear", "Select a year", c("All" = "all", "2010" = "2010", "2011" = "2011", "2012" = "2012", "2013" = "2013", "2014" = "2014", "2015" = "2015"), selected = "all"),
            checkboxInput("li", "Low-GDP countries", value = TRUE),
            checkboxInput("lm", "Lower-middle GDP countries", value = TRUE),
            checkboxInput("um", "Upper-middle GDP countries", value = TRUE),
            checkboxInput("hi", "High-GDP countries", value = TRUE),
            actionButton("submit_loc", "Submit"),
            h5("To plot the GDP (Gross Domestic Product) per capita Vs HDI, please select the year then the category(ies)."),
            h5("If you want to plot the data of all available years, select \"All\"."),
            h5("Click on the tab \"Data\" to see the corresponding data.")
            ),
        mainPanel(
            tabsetPanel(type = "tabs",
                        tabPanel("Plot", plotlyOutput("plot")),
                        tabPanel("Data", tableOutput("table")))
        )
    )
))

server.R

library(shiny)
library(plotly)
shinyServer(function(input, output) {
    observeEvent(eventExpr = input[["submit_loc"]], handlerExpr = {
    HDI <- read.csv("HDI.csv", skip = 1, head = TRUE, na.strings = c("NA", ""))
    colnames(HDI) <- sub("^X", "", colnames(HDI))
    HDI$Country <- trimws(HDI$Country)
    GDP <- read.csv("GDP.csv", skip = 1, head = TRUE, na.strings = c("NA", ""))
    colnames(GDP) <- sub("^X", "", colnames(GDP))
    GDP$Country <- trimws(GDP$Country)
    allData <- array(NA, dim = c(183, 3, 6), dimnames = list(c(), c("Country", "GDP", "HDI"), c("2010", "2011", "2012", "2013", "2014", "2015")))
    for (i in 1:6) {
        allData[,,i] <- cbind(HDI[HDI$Country %in% GDP$Country,][, 2], GDP[GDP$Country %in% HDI$Country,][,i+6], HDI[HDI$Country %in% GDP$Country,][, i+22])
    }
    inputYear <- input$selYear
    if(inputYear == "all") {
        tmp1 <- data.frame()
        for (i in 2010:2015) {
            tmp <- cbind(allData[,,as.character(i)], rep(i, dim(allData[,,as.character(i)])[1]))
            tmp1 <- rbind(tmp1, tmp, stringsAsFactors = FALSE)
        }
        colnames(tmp1)[4] <- "year"
        } else {
        tmp1 <- data.frame(allData[,,as.character(inputYear)], stringsAsFactors = FALSE)
        }
    tmp1 <- tmp1[complete.cases(tmp1),]
    tmp1$GDP <- as.numeric(tmp1$GDP)
    tmp1$HDI <- as.numeric(tmp1$HDI)
    if (!input$li) {tmp1 <- tmp1[tmp1$GDP > 1005,]}
    if (!input$lm) {tmp1 <- tmp1[tmp1$GDP < 1006 | tmp1$GDP > 3955,]}
    if (!input$um) {tmp1 <- tmp1[tmp1$GDP < 3956 | tmp1$GDP > 12235,]}
    if (!input$hi) {tmp1 <- tmp1[tmp1$GDP < 12236,]}
    output$plot <- renderPlotly({
        if (inputYear == "all") {
            p <- plot_ly(data = tmp1, x = ~year, y = ~GDP, z = ~HDI, color = ~year, text = ~Country) %>%
                layout(scene = list(yaxis = list(title = 'GDP ($ per person)'), xaxis = list(title = 'Year')))
        } else {
            p <- plot_ly(data = tmp1, x = ~GDP, y = ~HDI, text = ~Country) %>%
                layout(yaxis = list(range = c(0.3, 1)),
                       xaxis = list(title = 'GDP ($ per person)',
                                    range = c(0,140000)))
        }
    })
    output$table <- renderTable({
    tmp1
    })
    })
})

Shiny App

You can use my App by following this link, Thanks!