-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sanctuary_tab.R
172 lines (148 loc) · 7.48 KB
/
Sanctuary_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
163
164
165
166
167
168
169
170
171
172
library(tidyverse)
library(scales)
library(timetk)
library(knitr)
source("source_data.R")
# Calculations for the Sanctuary tab
### Create the base data set
Sanctuary_base <- Weekly_data |>
select(Date,
Total_Sanctuary,
Sanctuary,
SanctuaryOnline) |>
rename(Total = Total_Sanctuary,
In_person = Sanctuary,
Online = SanctuaryOnline)
# Sanctuary Main Table
Sanctuary_Last_week <- tail(Sanctuary_base, 1) |>
select(Total,
In_person,
Online)
# Weekly mean month-to-date for current month
Sanctuary_Month_To_Date <- Sanctuary_base |>
select(Date,
Total,
In_person,
Online) |>
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
Sanctuary_Year_over_year <- Sanctuary_base |>
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
Sanctuary_YoY_Month <- data.frame(Total = round(Sanctuary_Month_To_Date$Total - Sanctuary_Year_over_year$Total, 1),
In_person = round(Sanctuary_Month_To_Date$In_person - Sanctuary_Year_over_year$In_person, 1),
Online = round(Sanctuary_Month_To_Date$Online - Sanctuary_Year_over_year$Online, 1))
Sanctuary_YoY_Month <- Sanctuary_YoY_Month |>
mutate(Total_percent = Sanctuary_YoY_Month$Total / Sanctuary_Year_over_year$Total,
In_person_percent = Sanctuary_YoY_Month$In_person / Sanctuary_Year_over_year$In_person,
Online_percent = Sanctuary_YoY_Month$Online / Sanctuary_Year_over_year$Online)
# Year-to-date averages for current and past year
Sanctuary_By_week <- Weekly_data |>
mutate(Year = year(Date)) |>
mutate(Month = month(Date, label = TRUE))
Sanctuary_End_week <- tail(Sanctuary_By_week$Week, 1)
Sanctuary_Current_year <- tail(Sanctuary_By_week$Year, 1)
Sanctuary_Previous_year <- Sanctuary_Current_year - 1
Sanctuary_Current_month <- tail(Sanctuary_By_week$Month, 1)
Sanctuary_Attendance_past_year <- subset(Sanctuary_By_week, Year == Previous_year & Week <= End_week)
Sanctuary_Attendance_current_year <- subset(Sanctuary_By_week, Year == Sanctuary_Current_year)
Sanctuary_Weekly_ave_past_year <- Sanctuary_Attendance_past_year |>
group_by(Year) |>
summarise(Total = round(mean(Total_Sanctuary), 1),
In_person = round(mean(Sanctuary), 1),
Online = round(mean(SanctuaryOnline), 1))
Sanctuary_Weekly_ave_current_year <- Sanctuary_Attendance_current_year |>
group_by(Year) |>
summarise(Total = round(mean(Total_Sanctuary), 2),
In_person = round(mean(Sanctuary), 2),
Online = round(mean(SanctuaryOnline), 2))
# Sanctuary summary table
Sanctuary_main_table <- matrix(c(
Sanctuary_Last_week$Total,
Sanctuary_Last_week$In_person,
Sanctuary_Last_week$Online,
Sanctuary_Month_To_Date$Total,
Sanctuary_Month_To_Date$In_person,
Sanctuary_Month_To_Date$Online,
Sanctuary_Year_over_year$Total,
Sanctuary_Year_over_year$In_person,
Sanctuary_Year_over_year$Online,
Sanctuary_YoY_Month$Total,
Sanctuary_YoY_Month$In_person,
Sanctuary_YoY_Month$Online,
label_percent(accuracy = 0.1)(Sanctuary_YoY_Month$Total_percent),
label_percent(accuracy = 0.1)(Sanctuary_YoY_Month$In_person_percent),
label_percent(accuracy = 0.1)(Sanctuary_YoY_Month$Online_percent)),
ncol = 5,
byrow = FALSE
)
colnames(Sanctuary_main_table) <- c("Last week",
paste(Sanctuary_Current_month, Sanctuary_Current_year, sep = " "),
paste(Sanctuary_Current_month, Sanctuary_Previous_year, sep = " "),
"Change",
"Percent change")
rownames(Sanctuary_main_table) <- c("Combined",
"In person",
"Online")
# Year-to-date averages, absolute change, and percent change table
Sanctuary_ytd_change <- data.frame(Total = Sanctuary_Weekly_ave_current_year$Total - Sanctuary_Weekly_ave_past_year$Total,
In_person = Sanctuary_Weekly_ave_current_year$In_person - Sanctuary_Weekly_ave_past_year$In_person,
Online = Sanctuary_Weekly_ave_current_year$Online - Sanctuary_Weekly_ave_past_year$Online)
Sanctuary_ytd_change <- Sanctuary_ytd_change |>
mutate(Total_percent = Sanctuary_ytd_change$Total / Sanctuary_Weekly_ave_past_year$Total,
In_person_percent = Sanctuary_ytd_change$In_person / Sanctuary_Weekly_ave_past_year$In_person,
Online_percent = Sanctuary_ytd_change$Online / Sanctuary_Weekly_ave_past_year$Online)
Sanctuary_ytd_table <- matrix(c(Sanctuary_Weekly_ave_current_year$Total,
Sanctuary_Weekly_ave_current_year$In_person,
Sanctuary_Weekly_ave_current_year$Online,
Sanctuary_Weekly_ave_past_year$Total,
Sanctuary_Weekly_ave_past_year$In_person,
Sanctuary_Weekly_ave_past_year$Online,
round(Sanctuary_ytd_change$Total, 1),
round(Sanctuary_ytd_change$In_person, 1),
round(Sanctuary_ytd_change$Online, 1),
label_percent(accuracy = 0.1)(Sanctuary_ytd_change$Total_percent),
label_percent(accuracy = 0.1)(Sanctuary_ytd_change$In_person_percent),
label_percent(accuracy = 0.1)(Sanctuary_ytd_change$Online_percent)),
ncol = 4,
byrow = FALSE
)
colnames(Sanctuary_ytd_table) <- c(paste(Current_year),
paste(Previous_year),
"Change",
"Percent change")
rownames(Sanctuary_ytd_table) <- c("Combined",
"In person",
"Online")
# Data sets for reactive selection
## Sanctuary in person
Sanctuary_in_person <- Sanctuary_base |>
select(Date,
In_person) |>
group_by(Date = floor_date(Date, "month")) |>
summarize(attendance = round(mean(In_person, na.rm = TRUE), 2))
## Sanctuary online
Sanctuary_online <- Sanctuary_base |>
select(Date,
Online) |>
group_by(Date = floor_date(Date, "month")) |>
summarize(attendance = round(mean(Online, na.rm = TRUE), 2))
## Sanctuary combined attendance
Sanctuary_total <- Sanctuary_base |>
select(Date,
Total) |>
group_by(Date = floor_date(Date, "month")) |>
summarise(attendance = round(mean(Total, na.rm = TRUE), 2))