-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic_paths.R
More file actions
208 lines (191 loc) · 6.82 KB
/
logic_paths.R
File metadata and controls
208 lines (191 loc) · 6.82 KB
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
source("logic_terms.R")
# data prep ======
# convenience function to fill down columns
fill_down_columns <- function(df, cols) {
for (col in cols)
df <-
df |>
dplyr::mutate(..fill = cumsum(!is.na(!!sym(col)))) |>
dplyr::mutate(dplyr::across(!!col, ~.x[1]), .by = ..fill) |>
dplyr::select(-"..fill")
return(df)
}
# prepare classes
prepare_classes <- function(classes) {
classes <- classes |>
dplyr::mutate(
class = stringr::str_remove_all(.data$class, "[ \\r\\n]"),
inactive = !is.na(.data$inactive) & .data$inactive,
path_class = .data$class,
.after = "class"
)
# include 4000/5000 cross-listed classes also with the 4000 listing only
classes_4000 <- classes |>
dplyr::filter(stringr::str_detect(.data$class, "4\\d{3}/5\\d{3}")) |>
dplyr::mutate(
path_class = stringr::str_remove(.data$class, "/5\\d{3}")
)
# finalize
classes |>
dplyr::bind_rows(
classes_4000 |> dplyr::anti_join(classes, by = "path_class")
) |>
# make sure every class only exists once
dplyr::slice_head(n = 1L, by = "path_class") |>
# figure out level
dplyr::mutate(
program = stringr::str_extract(.data$class, "^[^0-9]+"),
number = stringr::str_extract(.data$class, "\\d+") |> as.integer(),
upper_division = !is.na(.data$number) & .data$number >= 3000 & .data$number < 5000
)
}
# prepare schedule for paths
prepare_schedule <- function(schedule) {
# safety checks
schedule |>
dplyr::mutate(
class = stringr::str_remove_all(class, "[ \\r\\n]"),
canceled = !is.na(.data$canceled) & .data$canceled,
confirmed = !is.na(.data$confirmed) & .data$confirmed,
room = .data$room |> stringr::str_remove("\\.\\d+$")
) |>
# remove deleted and canceled records
dplyr::filter(is.na(.data$deleted), !.data$canceled)
}
# prepare path recommmendation classes
prepare_path_recommendations <- function(paths) {
paths |>
fill_down_columns(c("path_id", "category")) |>
dplyr::mutate(
path = .data$path[1],
url = .data$url[1],
.by = c("path_id")
) |>
dplyr::mutate(
category_description = .data$category_description[1],
rec_min = .data$rec_min[1],
.by = c("path_id", "category")
) |>
dplyr::mutate(row = dplyr::row_number(), .before = 1L) |>
# path with ID
dplyr::mutate(
path_w_id = sprintf("%s (%s)", .data$path, .data$path_id)
) |>
# category info
dplyr::mutate(
n = stringr::str_sub(rec_min, 1, 1),
category_info = dplyr::case_when(
is.na(.data$n) ~ .data$category,
stringr::str_detect(.data$n, "\\d") ~ sprintf("%s (at least %s recommended)", .data$category, .data$n),
TRUE ~ sprintf("%s (take %s)", .data$category, .data$rec_min)
),
.after = "category"
)
}
# get all path classes
prepare_all_path_classes <- function(paths, classes) {
paths |>
dplyr::rename("path_class" = "class") |>
dplyr::left_join(classes, by = "path_class") |>
dplyr::mutate(
.by = c("path_id", "category"),
total = dplyr::n()
) |>
dplyr::mutate(
recommendation = ifelse(.data$n == "a", "take", sprintf("%s/%d", .data$n, .data$total))
) |>
dplyr::mutate(
.by = "class",
major_category =
if (any(!is.na(.data$category) & stringr::str_detect(.data$category, "[Rr]equired"))) "Required classes"
else if (any(!is.na(.data$category) & stringr::str_detect(.data$category, "[Ss]trongly"))) "Strongly recommended in some paths"
else if (any(!is.na(.data$category_info) & stringr::str_detect(.data$category_info, "[Rr]ecommended"))) "Recommended in some paths"
else if (!is.na(.data$program[1]) && .data$program[1] == "GEOL") "Other GEOL upper division classes"
else "Other upper division classes"
) |>
dplyr::mutate(
major_category = factor(.data$major_category) |> forcats::fct_relevel("Required classes", after = Inf)
) |>
dplyr::select("program", "class", "title", "credits", "path_id", "major_category", "recommendation") |>
tidyr::pivot_wider(names_from = path_id, values_from = recommendation) |>
dplyr::arrange(dplyr::desc(.data$major_category), dplyr::desc(.data$program == "GEOL"), .data$program, .data$class)
}
# get path specific list of classes
prepare_path_classes <- function(paths, selected_path, classes) {
path_classes <-
paths |>
dplyr::filter(path_w_id == !!selected_path) |>
dplyr::rename("path_class" = "class") |>
dplyr::left_join(classes, by = "path_class")
available_classes <-
# combine all classes
dplyr::bind_rows(
path_classes,
classes |>
dplyr::filter(.data$upper_division) |>
dplyr::anti_join(path_classes, by = c("path_class")) |>
dplyr::filter(.data$program == "GEOL") |>
dplyr::mutate(category_info = "Other GEOL upper division classes"),
classes |>
dplyr::filter(.data$upper_division) |>
dplyr::anti_join(path_classes, by = c("path_class")) |>
dplyr::filter(.data$program != "GEOL") |>
dplyr::mutate(category_info = "Other upper division classes")
) |>
dplyr::slice_head(n = 1, by = "class") |>
dplyr::mutate(
row = dplyr::row_number()
)
return(available_classes)
}
combine_path_classes_with_schedule <- function(path_classes, schedule, selected_terms) {
schedule_wide <-
schedule |>
dplyr::filter(term %in% !!selected_terms) |>
dplyr::mutate(
info = "yes",
term = factor(.data$term, levels = !!selected_terms)
) |>
dplyr::arrange(.data$term) |>
dplyr::slice_head(n = 1L, by = c("term", "class")) |>
tidyr::pivot_wider(id_cols = c(class), names_from = term, values_from = info)
path_classes |>
dplyr::left_join(schedule_wide, by = "class") |>
dplyr::mutate(
dplyr::across(
dplyr::any_of(!!selected_terms),
~dplyr::case_when(
!is.na(.x) ~ .x,
.data$program != "GEOL" ~ "?",
TRUE ~ "no")
)
)
}
prepare_all_paths_table_columns <- function(path_classes) {
path_classes |>
dplyr::mutate(
row = dplyr::row_number(),
Category = major_category,
Class = sprintf("%s(%s)", class, credits),
Title = title,
.before = 1L
) |>
dplyr::select(-"program", -"class", -"credits", -"title", -"major_category")
}
prepare_path_classes_table_columns <- function(path_classes) {
path_classes |>
dplyr::mutate(
Category =
ifelse(
!is.na(category_description),
sprintf("%s<br/><i>%s</i>",
htmltools::htmlEscape(category_info),
htmltools::htmlEscape(category_description)),
htmltools::htmlEscape(category_info)
),
Class = sprintf("%s(%s)", class, credits),
Title = title,
`Relevance for this path` = reason,
) |>
dplyr::select("row", "Category", "Class", "Title", "Relevance for this path", dplyr::matches(get_term_regexp()))
}