-
Notifications
You must be signed in to change notification settings - Fork 7
/
packDataPack.R
168 lines (144 loc) · 5.7 KB
/
packDataPack.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
#' @export
#' @title Pack a Data Pack
#'
#' @description
#' Takes a Data Pack template, combines it with data pulled from DATIM API, and
#' produces a Data Pack ready for distribution.
#'
#' @inheritParams datapackr_params
#'
#' @return Exports a Data Pack to Excel within \code{output_folder}.
#'
packDataPack <- function(model_data,
datapack_name,
country_uids,
template_path = NULL,
cop_year = getCurrentCOPYear(),
output_folder = NULL,
results_archive = TRUE,
d2_session = dynGet("d2_default_session",
inherits = TRUE)) {
interactive_print(datapack_name)
interactive_print(country_uids)
if (is.null(output_folder) || file.access(output_folder, 2) != 0) {
stop("Cannot write to output_folder")
}
# Create data train for use across remainder of program
d <- list(
keychain = list(
template_path = template_path,
output_folder = output_folder
),
info = list(
datapack_name = datapack_name,
sane_name = getSaneName(datapack_name),
country_uids = country_uids,
tool = "Data Pack",
cop_year = cop_year,
source_user = d2_session$me$userCredentials$username,
operating_unit = getOUFromCountryUIDs(country_uids)
),
data = list(
model_data = model_data
)
)
# Open schema ####
if (d$info$cop_year == 2021) {
d$info$schema <- datapackr::cop21_data_pack_schema
} else if (d$info$cop_year == 2022) {
d$info$schema <- datapackr::cop22_data_pack_schema
}
# Open template ####
# Grab correct schema
if (is.null(d$keychain$template_path)) {
if (cop_year == 2021) {
d$info$template_filename <- "COP21_Data_Pack_Template.xlsx"
} else if (cop_year == 2022) {
d$info$template_filename <- "COP22_Data_Pack_Template.xlsx"
}
d$keychain$template_path <- system.file("extdata",
d$info$template_filename,
package = "datapackr",
mustWork = TRUE)
}
d$keychain$template_path <- handshakeFile(path = d$keychain$template_path,
tool = "Data Pack Template")
# Test template against schema ####
interactive_print("Checking template against schema and DATIM...")
schema <-
unPackSchema_datapack(
template_path = d$keychain$template,
skip = skip_tabs(tool = "Data Pack Template", cop_year = cop_year),
cop_year = cop_year)
if (!identical(d$info$schema, schema)) {
stop("Ruh roh. Template provided does not match archived schema.")
}
# Place Workbook into play ####
if (is.null(d$tool$wb)) {
d$tool$wb <- openxlsx::loadWorkbook(d$keychain$template_path)
}
# Set global numeric format ####
options("openxlsx.numFmt" = "#,##0")
# Write Home Sheet info ####
d$tool$wb <- writeHomeTab(wb = d$tool$wb,
datapack_name = d$info$datapack_name,
country_uids = d$info$country_uids,
cop_year = cop_year,
tool = "Data Pack")
# Get PSNU List####
d$data$PSNUs <- datapackr::valid_PSNUs %>%
dplyr::filter(country_uid %in% country_uids) %>%
add_dp_psnu(.) %>%
dplyr::arrange(dp_psnu) %>%
## Remove DSNUs
dplyr::filter(!is.na(psnu_type)) %>%
dplyr::select(PSNU = dp_psnu, psnu_uid, snu1)
# TODO: Separate PSNUs as parameter for this function, allowing you to include
# a list of whatever org units you want. Sites, PSNUs, Countries, whatever.
# Write Main Sheets ####
d$tool$wb <- packDataPackSheets(wb = d$tool$wb,
country_uids = d$info$country_uids,
ou_level = "Prioritization",
org_units = d$data$PSNUs,
model_data = d$data$model_data,
schema = d$info$schema,
sheets = NULL,
cop_year = d$info$cop_year)
# Hide unneeded sheets ####
sheets_to_hide <- which(stringr::str_detect(names(d$tool$wb), "PSNUxIM"))
openxlsx::sheetVisibility(d$tool$wb)[sheets_to_hide] <- "hidden"
# Add Styles ####
interactive_print("Cleaning up Styles...")
## TODO: Address this in Data Pack?
# ## Add styles to Summary tab
# summaryStyle = openxlsx::createStyle(fgFill = "#404040")
# openxlsx::addStyle(d$tool$wb, sheet = "Summary",
# summaryStyle, cols = 1:2, rows = 1:62, gridExpand = TRUE, stack = TRUE)
## Add styles to Spectrum tab ####
#TODO: See if new openxlsx release addresses this issue
spectrumStyle1 <- openxlsx::createStyle(fgFill = "#9CBEBD")
spectrumStyle2 <- openxlsx::createStyle(fgFill = "#FFEB84")
openxlsx::addStyle(d$tool$wb,
sheet = "Spectrum",
spectrumStyle1,
cols = 1:3, rows = 1:40, gridExpand = TRUE, stack = TRUE)
openxlsx::addStyle(d$tool$wb,
sheet = "Spectrum",
spectrumStyle2, cols = 2, rows = 2, gridExpand = TRUE, stack = TRUE)
# Add validations
interactive_print("Adding Validations...")
#TODO: Adding validations prevents use of openxlsx to add SNU x IM tab
# Save & Export Workbook
interactive_print("Saving...")
exportPackr(data = d$tool$wb,
output_folder = d$keychain$output_folder,
tool = d$info$tool,
datapack_name = d$info$datapack_name)
# Save & Export Archive
if (results_archive) {
exportPackr(data = d,
output_folder = d$keychain$output_folder,
tool = "Results Archive",
datapack_name = d$info$datapack_name)
}
}