Last active
August 5, 2016 01:24
-
-
Save annecool37/0294e607d4ef8e5b7f702875b6ebe85c to your computer and use it in GitHub Desktop.
A glance at the code of Global Coffee Trade
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
# This part of the code shows how I create the line chart in the "Trend" tab | |
# Packages used: dplyr, shiny, shinydashboard, googleVis | |
# Overview: | |
# 1. create reactive object to read input from the ui function | |
# 2. render chart | |
# line chart (time series) to see how attributes change over time for a particular country | |
coffee_over_time = reactive({ | |
# validate if user select the desired input | |
validate( | |
need(input$checked != "", "Please check at least one variable in category"), | |
need(input$country_nm != "", "Please select a country"), | |
need(input$type_for_line != "", "Please select a unit") | |
) | |
# get the dataframe based on user selection | |
coffee_subset = coffee %>% filter(region == input$country_nm) | |
n = length(input$checked) | |
# check the year range based on country selected | |
df = data.frame("year" = min(coffee_subset$year): max(coffee_subset$year)) | |
for (i in 1:n) { | |
# filter df based on country selected | |
temp_df = filter(coffee_subset, Attribute_Description == input$checked[i]) | |
# select data with unit selected | |
temp_df = temp_df[,c(4, which(names(temp_df) == input$type_for_line))] | |
names(temp_df) = c("year", input$checked[i]) | |
df = full_join(df, temp_df) %>% arrange(year) | |
} | |
df = unique(df) | |
df | |
}) | |
# render line chart (time series) | |
output$line_chart = renderGvis( | |
gvisLineChart(coffee_over_time(), | |
option = list( | |
hAxis = "{title:'Year'}", | |
legend= "{position: 'bottom'}", | |
height = 400 | |
)) | |
) |
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
# This part of the code shows how I read user input | |
# and render line chart in the "Trend" tab | |
# Overview: | |
# 1. get user input with unique id to pass to server function | |
# 2. place the line chart created in the server into the tab | |
tabItem(tabName = "line", | |
# get user input | |
box(checkboxGroupInput("checked", "Category", | |
attribute_lst, | |
selected = attribute_lst[c(5,8)]), width = 4), | |
box(width = 4, selectizeInput("country_nm", "Country", | |
region_lst, | |
selected = "European Union")), | |
box(width = 4, selectizeInput("type_for_line", "Unit", | |
c("kg" = "kg","kg/gdp" = "kg_per_gdp", | |
"kg/person" = "kg_per_person"), | |
selected = "kg_per_gdp")), | |
# insert the line chart | |
box(status = "primary", solidHeader = TRUE, | |
title = textOutput("line_info"), | |
htmlOutput("line_chart", width = "100%"), width = 8) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment