-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_tab.R
162 lines (139 loc) · 6.18 KB
/
main_tab.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
library(tidyverse)
library(scales)
library(timetk)
source("source_data.R")
# Calculations for the main tab
### Main table calculations
# Get last week's attendance numbers
Last_week <- tail(Weekly_data, 1) |>
select(In_person,
Online,
Total)
# Calculate the month-to-date weekly average for the current month
Month_to_date <- Weekly_data |>
select(Date,
In_person,
Online,
Total) |>
group_by(floor_date(Date, "month")) |>
summarize(In_person = round(mean(In_person, na.rm = TRUE), 2),
Online = round(mean(Online, na.rm = TRUE), 2),
Total = round(mean(Total, na.rm = TRUE), 2)) |>
filter(row_number() == n())
# Calculate the weekly average for the same month the previous year
Year_over_year <- Weekly_data |>
select(Date,
In_person,
Online,
Total) |>
group_by(floor_date(Date, "month")) |>
summarize(In_person = round(mean(In_person, na.rm = TRUE), 2),
Online = round(mean(Online, na.rm = TRUE), 2),
Total = round(mean(Total, na.rm = TRUE), 2)) |>
slice_tail(n = 13) |>
slice_head(n = 1)
# Absolute year-over-year change and percent change
YoY_Month <- data.frame(Total = round(Month_to_date$Total - Year_over_year$Total, 1),
In_person = round(Month_to_date$In_person - Year_over_year$In_person, 1),
Online = round(Month_to_date$Online - Year_over_year$Online, 1))
YoY_Month <- YoY_Month |>
mutate(Total_percent = YoY_Month$Total / Year_over_year$Total,
In_person_percent = YoY_Month$In_person / Year_over_year$In_person,
Online_percent = YoY_Month$Online / Year_over_year$Online)
# Year-to-date averages for current and past year
By_week <- Weekly_data |>
mutate(Year = year(Date)) |>
mutate(Month = month(Date, label = TRUE))
End_week <- tail(By_week$Week, 1)
Current_year <- tail(By_week$Year, 1)
Previous_year <- Current_year - 1
Current_month <- tail(By_week$Month, 1)
Attendance_past_year <- subset(By_week, Year == Previous_year & Week <= End_week)
Attendance_current_year <- subset(By_week, Year == Current_year)
Weekly_ave_past_year <- Attendance_past_year |>
group_by(Year) |>
summarise(Total = round(mean(Total), 1),
In_person = round(mean(In_person), 1),
Online = round(mean(Online), 1))
Weekly_ave_current_year <- Attendance_current_year |>
group_by(Year) |>
summarise(Total = round(mean(Total), 2),
In_person = round(mean(In_person), 2),
Online = round(mean(Online), 2))
# Main summary table
main_table <- matrix(c(
Last_week$Total,
Last_week$In_person,
Last_week$Online,
Month_to_date$Total,
Month_to_date$In_person,
Month_to_date$Online,
Year_over_year$Total,
Year_over_year$In_person,
Year_over_year$Online,
YoY_Month$Total,
YoY_Month$In_person,
YoY_Month$Online,
label_percent(accuracy = 0.1)(YoY_Month$Total_percent),
label_percent(accuracy = 0.1)(YoY_Month$In_person_percent),
label_percent(accuracy = 0.1)(YoY_Month$Online_percent)),
ncol = 5,
byrow = FALSE
)
colnames(main_table) <- c("Last week",
paste(Current_month, Current_year, sep = " "),
paste(Current_month, Previous_year, sep = " "),
"Change",
"Percent change")
rownames(main_table) <- c("Combined",
"In person",
"Online")
# Year-to-date averages, absolute change, and percent change table
ytd_change <- data.frame(Total = Weekly_ave_current_year$Total - Weekly_ave_past_year$Total,
In_person = Weekly_ave_current_year$In_person - Weekly_ave_past_year$In_person,
Online = Weekly_ave_current_year$Online - Weekly_ave_past_year$Online)
ytd_change <- ytd_change |>
mutate(Total_percent = ytd_change$Total / Weekly_ave_past_year$Total,
In_person_percent = ytd_change$In_person / Weekly_ave_past_year$In_person,
Online_percent = ytd_change$Online / Weekly_ave_past_year$Online)
ytd_table <- matrix(c(Weekly_ave_current_year$Total,
Weekly_ave_current_year$In_person,
Weekly_ave_current_year$Online,
Weekly_ave_past_year$Total,
Weekly_ave_past_year$In_person,
Weekly_ave_past_year$Online,
round(ytd_change$Total, 1),
round(ytd_change$In_person, 1),
round(ytd_change$Online, 1),
label_percent(accuracy = 0.1)(ytd_change$Total_percent),
label_percent(accuracy = 0.1)(ytd_change$In_person_percent),
label_percent(accuracy = 0.1)(ytd_change$Online_percent)),
ncol = 4,
byrow = FALSE
)
colnames(ytd_table) <- c(paste(Current_year),
paste(Previous_year),
"Change",
"Percent change")
rownames(ytd_table) <- c("Combined",
"In person",
"Online")
# Data sets for reactive selection option
## In person
in_person_data <- Weekly_data |>
select(Date,
In_person) |>
group_by(Date = floor_date(Date, "month")) |>
summarize(attendance = round(mean(In_person, na.rm = TRUE), 2))
## Online
online <- Weekly_data |>
select(Date,
Online) |>
group_by(Date = floor_date(Date, "month")) |>
summarize(attendance = round(mean(Online, na.rm = TRUE), 2))
## Combined
combined <- Weekly_data |>
select(Date,
Total) |>
group_by(Date = floor_date(Date, "month")) |>
summarize(attendance = round(mean(Total, na.rm = TRUE), 2))