-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.R
110 lines (88 loc) · 3.61 KB
/
data.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
library(tidyverse)
districts <- c('51', '80', '87', '102')
default_district <- '102'
time_zone <- 'Etc/GMT-12'
color <- '#CD202C'
KPIs <- read.csv('KPIs.csv')
getKPIname <- function(var) {
KPIs %>%
filter(Variable == var) %>%
pull(Name)
}
getKPIlist <- function() {
KPIlist <- list()
categories <- unique(KPIs$Category)
for (category in categories) {
CategoryKPIs <- subset(KPIs, Category == category)
# KPIlist[[length(KPIlist) + 1]] <- setNames(CategoryKPIs$Variable, CategoryKPIs$Name)
KPIlist[[category]] <- setNames(CategoryKPIs$Variable, CategoryKPIs$Name)
}
return (KPIlist)
}
# getKPIlist()
get_filename <- function(district) {
return(paste0('clubs-', district, '-', substr(strptime(format(Sys.time(), tz = time_zone), '%Y-%m-%d'), 1, 12), '.csv'))
}
obtain_data_clubs <- function(district) {
ClubsURL <- paste0('http://dashboards.toastmasters.org/export.aspx?type=CSV&report=clubperformance~', district)
df <- read.csv(ClubsURL)
df <- df[-nrow(df), ]
filename <- get_filename(district)
write.csv(df, filename, row.names = FALSE)
print(paste0('Saving ', filename))
return(df)
}
load_data_clubs <- function(district) {
if (is.null(district)) {
district <- default_district
}
filename <- get_filename(district)
if (file.exists(filename)) {
df <- read.csv(filename)
print(paste0('Reading from local ', filename))
}
else {
df <- obtain_data_clubs(district)
print(paste0('Reading from source ', filename))
}
colnames(df) <- c('District', 'Division', 'Area', 'Number', 'Club', 'Status', 'Base', 'Members', 'Goals', 'Edu1', 'Edu2', 'Edu3', 'Edu4', 'Edu5', 'Edu6', 'Mem1', 'Mem2', 'Train1', 'Train2', 'Due1', 'Due2', 'List', 'Distinguished')
df$Status <- as.factor(df$Status)
levels(df$Status) <- c('Active', 'Low', 'Ineligible', 'Suspended')
df$Distinguished <- replace_na(df$Distinguished, 'Not Yet')
df$Distinguished <- as.factor(df$Distinguished)
levels(df$Distinguished) <- c('Not Yet', 'Distinguished', 'Select Distinguished', 'President\'s Distinguished')
df <- df %>%
mutate(
Area = paste0(Division, Area),
Number = str_pad(Number, width = 7, pad = '0'),
New = Mem1 + Mem2,
Retained = Members - New,
Retention = ifelse(Base == 0, 0, Retained / Base),
NetGrowth = Members - Base,
ClubGrowth = ifelse(Base == 0, 0, NetGrowth / Base),
Renewals = Due1 + Due2,
EduGoals = CountEduGoals(Edu1, Edu2, Edu3, Edu4, Edu5, Edu6),
MemGoals = CountMemGoals(Mem1, Mem2),
TrnGoals = CountTrnGoals(Train1, Train2),
AdmGoals = CountAdmGoals(Due1, Due2, List),
URL = paste0('https://www.toastmasters.org/Find-a-Club/', Number)
)
row.names(df) <- df$Number
return(df)
}
CountEduGoals <- function(Edu1, Edu2, Edu3, Edu4, Edu5, Edu6) {
Goals <- as.integer(Edu1 >= 4) + as.integer(Edu2 >= 2) + as.integer(Edu3 >= 2) + as.integer(Edu4 >= 2) + as.integer(Edu5 >= 1) + as.integer(Edu6 >= 1)
return(Goals)
}
CountMemGoals <- function(Mem1, Mem2) {
Goals <- as.integer(Mem1 >= 4) + as.integer(Mem2 >= 4)
return(Goals)
}
CountTrnGoals <- function(Train1, Train2) {
Goals <- as.integer((Train1 >= 4) & (Train2 >= 4))
return(Goals)
}
CountAdmGoals <- function(Due1, Due2, List) {
Goals <- as.integer((Due1 + Due2 >= 1) & (List >= 1))
return(Goals)
}