/
template_categorical_variables.R
259 lines (199 loc) · 7.63 KB
/
template_categorical_variables.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#' Describe categorical variables of a data table
#'
#' @description Describes categorical variables of a data table. Use if any columns are classified as categorical in table attributes template.
#'
#' @param path
#' (character) Path to the metadata template directory.
#' @param data.path
#' (character) Path to the data directory.
#' @param write.file
#' (logical; optional) Whether to write the template file.
#'
#' @return
#' \item{catvars_*}{Columns:
#' \itemize{
#' \item{attributeName: Column name}
#' \item{code: Categorical variable}
#' \item{definition: Definition of categorical variable}
#' }
#' }
#'
#' @details
#' \code{template_categorical_variables()} knows which columns of a table
#' are \code{categorical} based on their definition under the \code{class}
#' column of the attributes_*.txt template.
#'
#' Character encoding of metadata extracted directly from the tables are
#' converted to UTF-8 via \code{enc2utf8()}.
#'
#' @examples
#' \dontrun{
#' # Set working directory
#' setwd("/Users/me/Documents/data_packages/pkg_260")
#'
#' # For tables containing categorical variables as classified in the table attributes template
#' template_categorical_variables(
#' path = "./metadata_templates",
#' data.path = "./data_objects")
#' }
#'
#' @export
#'
template_categorical_variables <- function(
path,
data.path = path,
write.file = TRUE) {
message('Templating categorical variables ...')
# Validate arguments --------------------------------------------------------
validate_arguments(
fun.name = 'template_categorical_variables',
fun.args = as.list(environment()))
# Read templates and data ---------------------------------------------------
# Read all templates in path then parse the table attribute file names to get
# the corresponding data table names. Once all file names are known, re-read
# all templates and data files.
x <- template_arguments(path = path)$x
attribute_template_names <- stringr::str_subset(
names(x$template),
"(?<=attributes_).*(?=\\.txt)")
data_tables <- sapply(
attribute_template_names,
attribute_template_to_table,
data.path = data.path)
x <- template_arguments(
path = path,
data.path = data.path,
data.table = data_tables)$x
# Validate templates --------------------------------------------------------
x <- remove_empty_templates(x)
# Extract categorical variables ---------------------------------------------
# Categorical variables are classified in each data tables attribute
# template. For each categorical variable found, extract unique codes, except
# for declared missing value codes, and return in a long data frame.
r <- lapply(
seq_along(data_tables),
function(i, data_tables) {
# Get components
# Read cols as char to prevent data.table::fread() parsing numeric "" to
# NA which cannot be converted back to "" before writing the template.
d <- data.table::fread(
paste0(data.path, "/", data_tables[i]),
colClasses = "character")
attributes <- x$template[[names(data_tables)[i]]]$content
# Do not continue unless data and attributes have made it this far
if (is.null(d) | is.null(attributes)) {
return(NULL)
}
categorical_variables <- attributes$attributeName[
attributes$class == "categorical"]
missing_value_codes <- dplyr::select(
attributes, attributeName, missingValueCode)
categorical_variables_file_name <- stringr::str_replace(
names(data_tables)[i],
"attributes_",
"catvars_")
# Continue if categorical variables exist for this data table and if
# a categorical variables template doesn't already exist
if (length(categorical_variables) == 0) {
message("No categorical variables found.")
} else {
if (categorical_variables_file_name %in% names(x$template)) {
message(categorical_variables_file_name, " already exists!")
} else {
message(categorical_variables_file_name)
# Compile components for the categorical variables template
catvars <- dplyr::select(d, categorical_variables)
catvars <- tidyr::gather(catvars, "attributeName", "code")
catvars <- dplyr::distinct(catvars)
catvars <- dplyr::right_join(missing_value_codes, catvars, by = "attributeName")
# Remove missing value codes listed in the table attributes template
# since these will be listed in the EML metadata separately. NOTE:
# Because EAL templates use "" instead of NA, all "" from the template
# are converted to NA to facilitate matching.
use_i <- apply(
catvars,
1,
function(x) {
if (x[["missingValueCode"]] == "NA") {
x[["missingValueCode"]] <- NA_character_
}
missing_value_code <- x[["missingValueCode"]] %in% x[["code"]]
return(missing_value_code)
})
catvars <- catvars[!use_i, ]
# Tranform contents into the categorical variables template format
catvars$definition <- ""
catvars <- dplyr::select(catvars, -missingValueCode)
# Order results
catvars <- dplyr::arrange(catvars, attributeName, code)
# Encode extracted metadata in UTF-8
catvars$attributeName <- enc2utf8(as.character(catvars$attributeName))
catvars$code <- enc2utf8(as.character(catvars$code))
# List under "content" to accord with structure returned by
# template_arguments()
catvars <- list(content = catvars)
return(catvars)
}
}
},
data_tables = data_tables)
names(r) <- stringr::str_replace(
names(data_tables),
"attributes_",
"catvars_")
# Write to file -------------------------------------------------------------
if (write.file) {
for (i in names(r)) {
if (!is.null(r[[i]])) {
data.table::fwrite(
x = r[[i]]$content,
file = paste0(path, "/", enc2utf8(i)),
sep = "\t",
quote = FALSE,
na = "NA")
}
}
}
message("Done.")
# Return --------------------------------------------------------------------
return(r)
}
#' Convert attributes file name to the corresponding data table file name
#'
#' @param attributes.template
#' (character) Table attributes template file name, including file extension
#' @param data.path
#' (character) Path to the data directory
#'
#' @return
#' (character) Data table file name
#'
#' @keywords internal
#'
attribute_template_to_table <- function(attributes.template, data.path) {
table_regex <- paste0(
"^(?<!^attributes_|^catvars_)",
stringr::str_extract(
attributes.template,
"(?<=attributes_).*(?=\\.txt)"),
"\\.[:alpha:]*$")
table <- stringr::str_subset(dir(data.path), table_regex)
return(table)
}
#' Convert data table file name to the corresponding attributes file name
#'
#' @param data.table
#' (character) File name of data table, including file extension
#'
#' @return
#' (character) Table attributes file name
#'
#' @keywords internal
#'
table_to_attribute_template <- function(data.table) {
attribute_template <- paste0(
"attributes_",
stringr::str_remove(data.table, "\\.[:alpha:]*"),
".txt")
return(attribute_template)
}