-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod_basic.R
141 lines (121 loc) · 6.3 KB
/
mod_basic.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# Module for a graph
# Module to show some stats
basicstatUI <- function(id) {
ns <- NS(id)
fluidRow(
h3("No of Assets"),
br(),
p(
"This section shows some basic information about the portfolio."
),
plotOutput(ns("statplot")),
verbatimTextOutput(ns("balancetext"))
)
}
basicstatServer <- function(id, dt) {
moduleServer(id,
function(input, output, session) {
df <- reactive({
dt %>%
ungroup() %>%
arrange(id, report_date) %>%
group_by(id) %>%
slice_max(report_date, n = 1) %>%
mutate(yr = lubridate::year(origination_date))
})
output$statplot <- renderPlot({
req(!is.null(df))
withProgress(message = "Plotting some graphs",
detail = "Won't take long",
value = 0,
{
setProgress(value = 1, message = "1 of 3..")
p1 <-
df() %>%
group_by(yr, loan_status) %>%
summarise(no_of_loans = n()) %>%
ungroup() %>%
ggplot(aes(
x = yr,
y = no_of_loans,
label = no_of_loans,
fill = factor(
loan_status,
levels = c("0", "1"),
labels = c("Good", "Bad")
)
)) +
geom_col(position = "dodge") +
geom_text(aes(y = no_of_loans + 20), position = position_dodge(width = 1)) +
labs(title = "No. of assets by year",
x = "Year",
y = "#") +
theme_bw() +
theme(legend.position = "none")
setProgress(value = 2, message = "2 of 3..")
p2 <-
df() %>%
group_by(asset_type, loan_status) %>%
summarise(no_of_loans = n()) %>%
ungroup() %>%
ggplot(aes(
x = asset_type,
y = no_of_loans,
label = no_of_loans,
fill = factor(
loan_status,
levels = c("0", "1"),
labels = c("Good", "Bad")
)
)) +
geom_col(position = "dodge") +
geom_text(aes(y = no_of_loans + 20), position = position_dodge(width = 1)) +
labs(title = "No. of assets by asset type",
x = "Asset Type",
y = "#") +
theme_bw() +
theme(legend.position = "none")
setProgress(value = 3, message = "3 of 3..")
p3 <-
df() %>%
group_by(customer_type, loan_status) %>%
summarise(no_of_loans = n()) %>%
ungroup() %>%
ggplot(
aes(
x = customer_type,
y = no_of_loans,
label = no_of_loans,
fill = factor(
loan_status,
levels = c("0", "1"),
labels = c("Good", "Bad")
)
)
) +
geom_col(position = "dodge") +
geom_text(aes(y = no_of_loans + 20), position = position_dodge(width = 1)) +
labs(title = "No. of assets by customer type",
x = "Customer Type",
y = "#") +
theme_bw() +
theme(legend.position = "bottom",
legend.title = element_blank())
setProgress(value = 4, message = "Patching..")
p4 <- p1 / (p2 | p3)
setProgress(value = 5, message = "Done..")
})
p4
})
output$balancetext <- renderText({
paste0(
"Total assets (no.): ",
length(unique(df()$id)) ,
"\n",
"Total balance outstanding: ",
round(sum(df()$balance) / 1000000, 2),
" M"
)
})
})
}