R/replace_chars.R
dataset_replace_disallowed_chars.Rddataset_test() reports characters that the database does not allow, but
fixing them has been a manual edit for each one. This applies the known
replacements in util_disallowed_char_replacements() and reports whatever it
could not resolve.
dataset_replace_disallowed_chars(
dataset_id,
path_data = "data",
files = "metadata.yml",
dry_run = TRUE
)Identifier for a study, or a vector of them
Path to folder with data
Names of files within each dataset folder to process. Defaults to
metadata.yml only; see the section on data.csv below before adding it.
Report what would change without writing anything. Default
TRUE; pass FALSE to apply the replacements.
A data frame with one row per disallowed character found, with
columns dataset_id, file, line, char, code, replacement and
status. status is "replaced" when a replacement is known,
"no replacement known" otherwise, or "file not valid UTF-8" for a file
that was skipped. Returned invisibly when dry_run = FALSE.
Only the offending characters are rewritten; the rest of the file is left as
it was. In particular this does not round-trip metadata.yml through
write_metadata(), which would reflow the whole file and bury the change in
an unreviewable diff.
Characters with no known replacement are reported rather than guessed at.
Each is either a genuine character that should be added to the exceptions
argument of check_disallowed_chars(), or a mangling that only the curator
can interpret.
A file that is not valid UTF-8 is reported and skipped, since recovering one means knowing which encoding it was written in.
data.csvdata.csv is not in files by default. It can be processed by asking for
it, but the two files are very different problems, and the scope for data is
still an open question (see #251).
dataset_test() applies its ASCII-only rule for data to
parsed_data$traits$value — the values that actually reach the built
database. This function reads whole files, so for data.csv it sees every
column, including ones that never become a trait value. Measured over all 601
downstream datasets, that is the difference between 95 disallowed
characters in metadata.yml and 170,946 in data.csv across 147 files —
almost none of which dataset_test() reports. The bulk is invisible junk in
provenance columns (110,666 zero-width joiners in one created_by column)
plus legitimate typography and accented names that the ASCII-only rule can
only ever report, never resolve.
So pass files = c("metadata.yml", "data.csv") deliberately, and read the
dry-run report first.
if (FALSE) { # \dontrun{
# See what would change across every dataset
report <- dataset_replace_disallowed_chars(dir("data"))
# Apply it
dataset_replace_disallowed_chars(dir("data"), dry_run = FALSE)
# Include the data files, which is a much wider sweep -- read the report first
dataset_replace_disallowed_chars(dir("data"), files = c("metadata.yml", "data.csv"))
} # }