-
-
Save mwp42/97e02260e5599ce656ade53208aa93ee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#global.R | |
library(shiny) | |
library(dplyr) | |
library(ggplot2) | |
library(shinydashboard) | |
library(rworldmap) | |
library(googleVis) | |
library(DT) | |
commodity <- read.csv(file = "./commodity_trade_statistics_data.csv") | |
#Top_4 - USA, China, Canada, Mexico | |
commodity_T4 <- commodity %>% | |
filter(., country_or_area == "USA" | country_or_area == "China, Hong Kong SAR" | country_or_area == "Canada" | country_or_area == "Mexico") | |
#tidy data | |
commodity_T4[,'comm_code']<-NULL | |
commodity_T4[,'weight_kg']<-NULL | |
commodity_T4[,'quantity_name']<-NULL | |
commodity_T4[,'quantity']<-NULL | |
commodity_T4 <- rename(commodity_T4, country = `country_or_area`) | |
commodity_T4 <- rename(commodity_T4, trade.in.usd = `trade_usd`) | |
commodity_T4 <- rename(commodity_T4, year.trade = `year`) | |
class(commodity_T4$category) ##factor | |
commodity_T4$category<-as.character(commodity_T4$category) | |
commodity_T4 <- mutate(commodity_T4, flow = gsub(pattern = "Re-Export", replacement = "Export", x = flow, ignore.case = F, fixed = T)) %>% | |
mutate(flow = gsub(pattern = "Re-Import", replacement = "Import", x = flow, ignore.case = F))%>% | |
mutate(country = gsub(pattern = "China, Hong Kong SAR", replacement = "China", x = country, ignore.case = F)) | |
## Data Set for Map## | |
commodity_T4_maps<-commodity_T4%>% | |
filter(.,year.trade==2016)%>% | |
mutate(.,flow_sign = ifelse(flow=="Import",-1,1))%>% | |
mutate(.,trade.in.sign = trade.in.usd*flow_sign)%>% | |
group_by(.,country)%>% | |
summarize(., balance = sum(trade.in.sign), balance2 = mean(trade.in.sign)) | |
#ui.R | |
sidebar <- dashboardSidebar( | |
width = 250, | |
sidebarUserPanel("Michael Tarino"), | |
sidebarMenu( | |
menuItem("Maps", tabName = "maps", icon = icon("fire")), | |
menuItem("Graphs", tabName = "graphs", icon = icon("list-alt"), | |
menuItem("Bar Graph: Trade", tabName = "bargraph", icon=icon('bar-chart-o')), | |
menuItem("Histogram: Trade", tabName = "histogram", icon=icon('bar-chart-o')) | |
), | |
menuItem("Statistics", tabName = "statistics", icon = icon("gear")), | |
menuItem("Table", tabName = "table", icon = icon("table")) | |
) | |
) | |
body <- dashboardBody( | |
tabItems( | |
tabItem(tabName = "maps", | |
h2("Global Commodity Trade Balances for 2016"), | |
fluidPage(htmlOutput("map")) | |
), | |
tabItem(tabName = "bargraph", | |
h2("Global Commodity Trade Data"), | |
fluidPage( | |
titlePanel("Bar Graph: Trade by Country and Flow"), | |
sidebarLayout( | |
sidebarPanel( | |
selectizeInput(inputId = "year.trade", | |
label = "Year", | |
choices = unique(commodity_T4[, 'year.trade'])), | |
selectizeInput(inputId = "category", | |
label = "Category", | |
choices = unique(commodity_T4[, 'category'])) | |
), | |
mainPanel( | |
fluidRow( | |
column(6, plotOutput("bar_graph", width = "200%")) | |
) | |
) | |
) | |
) | |
), | |
tabItem(tabName = "histogram", | |
h2("Global Commodity Trade Data"), | |
fluidPage(titlePanel("Histogram: Sum of Commodity transaction grouped by Year")), | |
fluidRow(column(6, plotOutput("histogram_graph", width = "200%"))) | |
), | |
tabItem(tabName = "statistics", | |
h2("Statistics: Linear Model and LOESS Regression"), | |
fluidRow(column(3, selectizeInput('country', | |
label='Country:', | |
choices= unique(commodity_T4[, 'country'])))), | |
fluidRow(column(5, plotOutput('statistics', width = "250%"))) | |
), | |
tabItem(tabName = "table", | |
h2("Table from Commodity Database"), | |
fluidRow(column(3, selectizeInput('country1', label='Country:', choices= unique(commodity_T4[, 'country']))), | |
column(3, selectizeInput('year.trade1', label='Year:', choices= unique(commodity_T4[, 'year.trade']))), | |
dataTableOutput('table')) | |
) | |
) | |
) | |
dashboardPage(skin='red', | |
dashboardHeader(title = "Commodity Data"), | |
sidebar, | |
body | |
) | |
#server.R | |
function(input, output, session) { | |
######## MAP ######## | |
output$map <- renderGvis({ | |
map<-gvisGeoChart(commodity_T4_maps, locationvar='country', colorvar='balance', | |
options=list(title='Trade Balance by Country', width='100%', projection="kavrayskiy-vii", colorAxis="{colors:['#0000ff', '#FF0000']}")) | |
return(map) | |
}) | |
######## BAR GRAPH 1 ######## | |
observe( | |
print(paste("year.trade:", input$year.trade, "category:", input$category)) | |
) | |
observe({ | |
category <- unique(commodity_T4 %>% | |
filter(commodity_T4$year.trade == input$year.trade) %>% | |
.$category) | |
updateSelectizeInput( | |
session, "category", | |
choices = category, | |
selected = category[1]) | |
}) | |
output$bar_graph <- renderPlot({ | |
commodity_T4 %>% | |
filter(.,commodity_T4$year.trade == input$year.trade & commodity_T4$category == input$category)%>% | |
group_by(.,country, flow)%>% | |
summarize(., sum_trade_yr = sum(trade.in.usd, na.rm=TRUE)) %>% | |
ggplot(., aes(x=flow, y=sum_trade_yr)) + | |
geom_bar(aes(x=country,fill=flow), stat="identity", position='dodge') + | |
labs(title='Trade by country and flow', x='Country', y='Sum Trade in USD') + | |
theme_bw() | |
}) | |
######## HISTOGRAM 1 ######## | |
output$histogram_graph <- renderPlot({ | |
commodity_T4%>% | |
group_by(country, year.trade)%>% | |
summarize(sum.trade=sum(trade.in.usd))%>% | |
ggplot(aes(x=sum.trade)) + geom_histogram(aes(fill=country), color="black", bins = 40, position = position_stack(reverse=TRUE)) + coord_cartesian(xlim=c(66000000000,5e+12),ylim=c(0,15)) + ##from 5000000, 4500000000 | |
xlab("Sum of Commodity transactions in USD grouped by Year") + ylab("Count") | |
}) | |
######## STATISTICS ######## | |
observe( | |
print(paste("country:", input$country)) | |
) | |
output$statistics <- renderPlot({ | |
commodity_T4 %>% | |
filter(.,commodity_T4$country == input$country, year.trade==1996|year.trade==1998|year.trade==2000|year.trade==2002|year.trade==2004|year.trade==2006|year.trade==2008|year.trade==2010|year.trade==2012|year.trade==2014|year.trade==2016)%>% | |
group_by(., year.trade)%>% | |
summarize(., balance = sum(trade.in.usd), balance2 = mean(trade.in.usd))%>% | |
ggplot(., aes(x=year.trade, y=balance)) + geom_point(color='red1', shape=2 , size=4) + geom_smooth(se=FALSE, col = "green") + stat_smooth(method = "lm", col = "blue", se=FALSE) + | |
labs(title='Trade Volume in USD', x='Year', y='Trade in USD') + | |
theme(legend.key=element_blank(), legend.position="bottom") + | |
theme_bw() | |
}) | |
######## FILTERING TABLE ######## | |
observe( | |
print(paste("year.trade:", input$year.trade1, "country:", input$country1)) | |
) | |
observe({ | |
country <- unique(commodity_T4 %>% | |
filter(commodity_T4$year.trade == input$year.trade1) %>% | |
.$country1) | |
updateSelectizeInput( | |
session, "country", | |
choices = country, | |
selected = country[1]) | |
}) | |
trade_filter<-reactive({ | |
commodity_T4%>% | |
filter(., year.trade == input$year.trade1)%>% | |
filter(., country == input$country1) | |
}) | |
output$table <- renderDataTable({ | |
datatable(trade_filter(), rownames=TRUE) %>% | |
formatStyle(input$selected, background="red", fontWeight='bold') | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment