-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
119 lines (103 loc) · 3.99 KB
/
server.R
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
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library(shiny)
# Load required packages
library(readr)
library(dplyr)
library(lubridate)
library(rCharts)
# Load the dataset created by data-processing.R
housing_finance <- readr::read_csv(file.path("data", "housing-finance.csv")) %>%
mutate(
data_month = as.Date(data_month),
series_id = factor(series_id),
data_series = factor(data_series),
borrower_type = factor(borrower_type),
purpose = factor(purpose)
)
shinyServer(function(input, output) {
output$financeByBorrower <- renderChart2({
hf_subset_aggregate <- housing_finance %>%
filter(
data_series == input$data_series,
year(data_month) >= input$timeline[1],
year(data_month) <= input$timeline[2]
) %>%
group_by(borrower_type, data_month) %>%
summarise(commitments = sum(commitments, na.rm = TRUE))
if(input$chart_display == "line") {
hf_total <- hf_subset_aggregate %>%
group_by(data_month) %>%
summarise(commitments = sum(commitments, na.rm = TRUE)) %>%
mutate(borrower_type = "Total")
hf_subset_aggregate <- bind_rows(hf_subset_aggregate, hf_total)
housing_finance_by_borrower <- nPlot(
commitments ~ data_month,
group = "borrower_type",
data = hf_subset_aggregate,
type = "lineChart"
)
housing_finance_by_borrower$set(width = 640)
housing_finance_by_borrower$xAxis(tickFormat = "#!function(d) {return d3.time.format('%Y-%m')(new Date( d * 86400000 ));}!#")
housing_finance_by_borrower$chart(forceY = c(0, max(hf_subset_aggregate$commitments)))
}
else {
hf_subset_aggregate <- hf_subset_aggregate %>%
group_by(data_month, borrower_type) %>%
summarise(commitments = commitments) %>%
mutate(prop = commitments / sum(commitments))
housing_finance_by_borrower <- nPlot(
prop ~ data_month,
group = "borrower_type",
data = hf_subset_aggregate,
type = "stackedAreaChart"
)
housing_finance_by_borrower$set(width = 640)
housing_finance_by_borrower$xAxis(tickFormat = "#!function(d) {return d3.time.format('%Y-%m')(new Date( d * 86400000 ));}!#")
housing_finance_by_borrower$chart(forceY = c(0, 1))
}
return(housing_finance_by_borrower)
})
output$financeByPurpose <- renderChart2({
hf_subset <- housing_finance %>%
filter(
data_series == input$data_series,
year(data_month) >= input$timeline[1],
year(data_month) <= input$timeline[2]
)
if(input$chart_display == "line") {
hf_total <- hf_subset %>%
group_by(data_month) %>%
summarise(commitments = sum(commitments, na.rm = TRUE)) %>%
mutate(purpose = "Total")
hf_subset <- bind_rows(hf_subset, hf_total)
housing_finance_by_purpose <- nPlot(
commitments ~ data_month,
group = "purpose",
data = hf_subset,
type = "lineChart"
)
housing_finance_by_purpose$set(width = 640)
housing_finance_by_purpose$xAxis(tickFormat = "#!function(d) {return d3.time.format('%Y-%m')(new Date( d * 86400000 ));}!#")
housing_finance_by_purpose$chart(forceY = c(0, max(hf_subset$commitments)))
} else {
hf_subset <- hf_subset %>%
group_by(data_month, purpose) %>%
summarise(commitments = commitments, na.rm = TRUE) %>%
mutate(prop = commitments / sum(commitments, na.rm = TRUE))
housing_finance_by_purpose <- nPlot(
prop ~ data_month,
group = "purpose",
data = hf_subset,
type = "stackedAreaChart"
)
housing_finance_by_purpose$set(width = 640)
housing_finance_by_purpose$xAxis(tickFormat = "#!function(d) {return d3.time.format('%Y-%m')(new Date( d * 86400000 ));}!#")
housing_finance_by_purpose$chart(forceY = c(0, 1))
}
return(housing_finance_by_purpose)
})
})