Skip to content

Commit

Permalink
add in survey to rename helper for #15
Browse files Browse the repository at this point in the history
  • Loading branch information
aspina7 committed Dec 16, 2021
1 parent 8f2d739 commit f7626a6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
54 changes: 42 additions & 12 deletions R/msf_dict_rename_helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,68 @@
#'
#' @export
#' @param disease Specify which disease you would like to use.
#' Currently supports "Cholera", "Measles" and "Meningitis", "AJS".
#' Currently supports "Cholera", "Measles", "Meningitis", "AJS",
#' "Mortality", "Nutrition", "Vaccination_short" and "Vaccination_long".
#'
#' @param varnames Specify name of column that contains varnames. Currently
#' default set to "Item". (this can probably be deleted once dictionaries
#' standardise) If `dictionary` is "Mortality", `varnames` needs to be "column_name"`.
#'
#' default set to "data_element_shortname".
#' If `dictionary` is a survey ("Mortality", "Nutrition", "Vaccination_short"
#' or "Vaccination_long") `varnames` needs to be "name"`.
#'
#' @param copy_to_clipboard if `TRUE` (default), the rename template will be
#' copied to the user's clipboard with [clipr::write_clip()]. If `FALSE`, the
#' rename template will be printed to the user's console.
#'
#' @return a dplyr command used to rename columns in your data frame according
#' to the dictionary
msf_dict_rename_helper <- function(disease, varnames = "data_element_shortname", copy_to_clipboard = TRUE) {
# get msf disease specific data dictionary
dat_dict <- msf_dict(disease = disease, compact = TRUE)

# make disease name lower case
disease <- tolower(disease)

# get msf disease specific outbreak data dictionary
if (disease == "cholera" | disease == "measles" |
disease == "meningitis" | disease == "ajs") {

dat_dict <- msf_dict(disease = disease, compact = TRUE)

# define the name of the column in the dictionary which has variable class
var_type_col <- "data_element_valuetype"
}

# get msf disease specific survey data dictionary
if (disease == "mortality" | disease == "nutrition" |
disease == "vaccination_short" | disease == "vaccination_long") {

dat_dict <- msf_dict_survey(disease, compact = TRUE)

# define the name of the column in the dictionary which has variable class
var_type_col <- "type"
}

# change vaccinations to be one
if (disease == "vaccination_short" | disease == "vaccination_long") {

disease <- "vaccination"
}

# get the outbreak Rmd to check if the variable is optional or required
outbreak_file <- available_sitrep_templates(recursive = TRUE, pattern = ".Rmd", full.names = TRUE)
outbreak_file <- grep(tolower(disease), outbreak_file, value = TRUE)[[1]]
outbreak_file <- grep(disease, outbreak_file, value = TRUE)[[1]]
outbreak_file <- readLines(outbreak_file)

dat_dict[["var_required"]] <- vapply(dat_dict[[varnames]],
FUN = function(i, o) if (any(grepl(paste0("^[^#]*", i), o))) "REQUIRED" else "optional",
FUN.VALUE = character(1),
dat_dict[["var_required"]] <- vapply(dat_dict[[varnames]],
FUN = function(i, o) if (any(grepl(paste0("^[^#]*", i), o))) "REQUIRED" else "optional",
FUN.VALUE = character(1),
o = outbreak_file
)
dat_dict <- dat_dict[order(dat_dict[["var_required"]] != "REQUIRED",
dat_dict <- dat_dict[order(dat_dict[["var_required"]] != "REQUIRED",
dat_dict[[varnames]]), ]
msg <- "## Add the appropriate column names after the equals signs\n\n"
msg <- paste0(msg, "linelist_cleaned <- rename(linelist_cleaned,\n")
the_renames <- sprintf(" %s = , # %s (%s)",
format(dat_dict[[varnames]]),
format(dat_dict[["data_element_valuetype"]]),
format(dat_dict[[var_type_col]]),
dat_dict[["var_required"]]
)
the_renames[length(the_renames)] <- gsub(",", " ", the_renames[length(the_renames)])
Expand Down
8 changes: 5 additions & 3 deletions man/msf_dict_rename_helper.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f7626a6

Please sign in to comment.