-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-Lab6.Rmd
231 lines (201 loc) · 6.5 KB
/
06-Lab6.Rmd
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<style type="text/css">
@import url("css/custom_lab6.css");
</style>
# Reporting: tables
**Scenario.** Lucy seems intrigued your presentation on Australia's sales. Now, she wants to review the sales difference (growth) for **Product Category** and **Sub Category** between year 2015 and 2016. She wants to be able to filter the report by all the options available, including **Customer Gender** and **Age Group**.
Load libraries
```{r}
library(tidyverse, warn.conflicts = F)
library(scales)
library(formattable)
library(htmlwidgets)
library(sparkline)
```
Load data for this lab:
```{r}
my_data <- readRDS("./data/processing/data4week3.rds")
str(my_data)
```
In this Lab we'll be review the sales difference (growth) for **Product Category** and **Sub Category** between year 2015 and 2016. So, we will create two type of reports. One with the sales for each year and the other with the **% Change in Revenue** from year to year. Each report is a cross-tabular format with **Product Categories** and **Sub categories** on the rows and year on the columns, with **Sum of Revenue** as the aggregate data. Also we will create chart representation for each report.
Let's start! First, create two reports just like on picture below
![](./img/mod6-lab6-img2.png)
For this task we're going to use such packages as [formattable](https://github.com/renkun-ken/formattable) and [sparklines](https://github.com/htmlwidgets/sparkline). Also we'll include external [css]("custom.css") file for styling our tables.
---
## Helper functions for type 1 report
### Count totals for Product Category
```{r}
product_total <- function(df) {
df %>%
group_by(`Product Category`, Year) %>%
summarise(Total = sum(Revenue)) %>%
mutate(Margin = (Total - lag(Total)) / Total) %>%
ungroup() %>%
mutate(`Sub Category` = `Product Category`) %>%
select(`Product Category`, `Sub Category`, everything())
}
```
```{r echo=FALSE}
my_data %>% product_total() %>% kable()
```
### List of unique product in Product Category
```{r}
uniq_product <- function(df) {
unique(df$`Product Category`)
}
my_data %>% uniq_product()
```
### Count totals for Sub Category:
```{r}
sub_total <- function(df) {
df %>%
group_by(`Product Category`, `Sub Category`, Year) %>%
summarise(Total = sum(Revenue)) %>%
mutate(Margin = (Total - lag(Total)) / Total) %>%
ungroup()
}
```
```{r echo=FALSE}
my_data %>%
sub_total() %>%
slice(1:10) %>%
kable()
```
### Prepare report_1
```{r}
report_1 <- function(df) {
bind_rows(product_total(df), sub_total(df)) %>%
select(-Margin) %>%
group_by(`Product Category`, Year) %>%
spread(key = Year, value = Total) %>%
group_by(`Product Category`) %>%
arrange(desc(`2016`), .by_group = TRUE) %>%
ungroup() %>%
select(-`Product Category`) %>%
mutate_if(is.numeric, formattable::comma, digits = 0)
}
my_data %>% report_1
```
### Define function that makes sparklines in formattable object
```{r}
my_sparkline <- function(x){
as.character(
htmltools::as.tags(
sparkline(x) # we can add any chart type!
)
)
}
```
### Make tibble with sparklines as html-widgets:
```{r}
sparkl_df <- function(df) {
report_1(df) %>%
nest(`2011`:`2016`, .key = 'Sparkl') %>%
mutate_at('Sparkl', map, as.numeric, my_sparkline) %>%
mutate_at('Sparkl', map, my_sparkline)
}
```
### Add sparklines objects to report_1:
```{r}
report_1_sparkl <- function(df) {
bind_cols(report_1(df), sparkl_df(df) %>%
select(Sparkl))
}
my_data %>% report_1_sparkl
```
### Final table for the first report
```{r}
report_1_table <- function(df) {
formattable(report_1_sparkl(df),
align = c("l", "r", "r", "r", "r", "r", "r"), table.attr = "class=\'my_table\'",
list(
area(, `2011`:`2016`) ~ color_tile("#FFFF00", "#FF0000"),
`Sub Category` = formatter("span",
style = x ~ ifelse(x %in% uniq_product(df),
style(font.weight = "bold"),
style(padding.left = "0.25cm"))
)
)
) %>%
formattable::as.htmlwidget() %>%
htmltools::tagList() %>%
htmltools::attachDependencies(htmlwidgets:::widget_dependencies("sparkline","sparkline")) %>%
htmltools::browsable()
}
my_data %>% report_1_table()
```
---
## Helper functions for report type 2
### Calculate changes in totals by year for product category
```{r}
report_2 <- function(df) {
bind_rows(product_total(df), sub_total(df)) %>%
select(-Total) %>%
group_by(`Product Category`, Year) %>%
spread(key = Year, value = Margin) %>%
ungroup() %>%
arrange(match(`Sub Category`, report_1(df)$`Sub Category`)) %>%
select(-`Product Category`, -`2011`) %>%
mutate_if(is.numeric, formattable::percent, digits = 1)
}
my_data %>% report_2
```
### Final table for the second report
```{r}
report_2_table <- function(df) {
formattable(report_2(df), align = c("l", "l", "l", "l", "l", "l"),
table.attr = "class=\'my_table\'",
list(
area(, `2012`:`2016`) ~ formatter("span",
style = x ~ style(color = ifelse(x < 0 , "red", "green")),
x ~ icontext(ifelse(x < 0, "arrow-down", "arrow-up"), formattable::percent(x, digits = 1))),
`Sub Category` = formatter("span",
style = x ~ ifelse(x %in% uniq_product(df),
style(font.weight = "bold"),
style(padding.left = "0.25cm")
)
)
)
)
}
my_data %>% report_2_table()
```
---
## Questions
### Without applying any filter, which year does the Accessories category have negative growth?
**Answer:** 2015
---
### Filter the reports for youth age group. Which two years do the accessories category have negative growth?
**Report 1**
```{r}
my_data %>%
filter(`Age Group` == "Youth") %>%
report_1_table()
```
**Report 2**
```{r}
my_data %>%
filter(`Age Group` == "Youth") %>%
report_2_table()
```
**Answer:** 2014, 2016
---
## Without applying any filter, which two years do the bikes category have negative growth?
**Answer:** 2014, 2016
---
## Filter the report for Australia. Which year do the bikes category have the highest growth?
**Report 1**
```{r}
my_data %>%
filter(Country == "Australia") %>%
report_1_table()
```
**Report 2**
```{r}
my_data %>%
filter(Country == "Australia") %>%
report_2_table()
```
**Answer:** 2015
---
## Keep the Australia filter. In the year that bikes sales have the highest growth (previous question), which sub category of bikes has the highest growth?
**Answer:** Mounting Bikes, 71%