Skip to content

Commit

Permalink
helper fx to create file description
Browse files Browse the repository at this point in the history
  • Loading branch information
aspina7 committed Sep 7, 2024
1 parent 0f3291c commit 2b5a46c
Show file tree
Hide file tree
Showing 14 changed files with 481 additions and 13 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Imports:
clipr
Suggests:
ggplot2,
palmerpenguins,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Generated by roxygen2: do not edit by hand

export(create_desc)
export(list_datasets)
importFrom(clipr,write_clip)
124 changes: 122 additions & 2 deletions R/AJS_chad_doc.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,128 @@
#'
#' @format A data frame with 1477 rows and 122 columns:
#' \describe{
#' \item{variable1}{explanation of a variable}
#' \item{variable2}{more explanation of variables}
#' \item{Number}{a numeric denoting HEV Count}
#' \item{HEVID}{a numeric denoting HEV Case ID}
#' \item{Sex}{a character denoting Sexe [M=Male; F=Femelle]}
#' \item{Age}{a character denoting Age}
#' \item{Agegroup}{a character denoting Groupe D'age [1=0 to 4; 2= 5 to 14; 3=15 to 44; 4==45]}
#' \item{dateentry}{a character denoting Initial Entry into Database (either Questionnaire or Medical Assessment)}
#' \item{epiquestdate}{a character denoting Date de Questionnaire}
#' \item{team}{a character denoting Equipe}
#' \item{chw}{a character denoting AC}
#' \item{block}{a character denoting Bloc}
#' \item{hh}{a character denoting Menage}
#' \item{referredby}{a character denoting Referer par: (1=Hôpital ; 2 = Superviseur Agent Communitaire)}
#' \item{quartier}{a character denoting Quartier/Village}
#' \item{interviewee}{a character denoting Statut interroge [Patient ou Parent]}
#' \item{hhpers}{a character denoting Nombre de personnes dan le menage}
#' \item{hhmale}{a character denoting Nombre masculin}
#' \item{hhfemale}{a character denoting Nombre feminin}
#' \item{hhu5}{a character denoting Nombre d'enfant < 5 ans}
#' \item{hpreferral}{a character denoting Référé à l’hôpital par Agent Com? (O = Oui, N = Non)}
#' \item{hprefpos}{a character denoting Si oui, le patient est-allé?}
#' \item{patientpresent}{a character denoting Est-ce que le patient est présent ?}
#' \item{ptjaundice}{a character denoting Est-ce que le patient a la jaunisse?}
#' \item{ptvomit}{a character denoting Vomissements ?}
#' \item{mtmental}{a character denoting Etat mental altéré ?}
#' \item{ptpregnant}{a character denoting Enceinte?}
#' \item{ptu1}{a character denoting Agée moins d’un an ?}
#' \item{referring}{a character denoting 15. Allez-vous référer à l’hôpital?}
#' \item{otherajs}{a character denoting Autre patient dans le foyer?}
#' \item{otherajsnum}{a character denoting Si oui, combien?}
#' \item{borehole}{a character denoting Forage?}
#' \item{comtap}{a character denoting Bonne Fontaine?}
#' \item{river}{a character denoting Riviere?}
#' \item{citywater}{a character denoting Eau de la ville?}
#' \item{otherwater}{a character denoting Autre}
#' \item{otherwatersp}{a character denoting Si autre…}
#' \item{watersourcefreq}{a character denoting Quel point utilisez-vous le plus souvent?}
#' \item{ceramicjar}{a character denoting Jarre}
#' \item{jerrycan}{a character denoting Jerrycan}
#' \item{oildrum}{a character denoting Fut}
#' \item{reserother}{a character denoting Autre?}
#' \item{reserothersp}{a character denoting Si autre….}
#' \item{resertype}{a character denoting 20. Type de stockage}
#' \item{chlorination}{a character denoting 21. Ajouté chlore la derniere fois?}
#' \item{volumreser}{a character denoting 22a. Combien de stockage?}
#' \item{handreser}{a character denoting 22b. Entrer la main dans le stockage?}
#' \item{hweating}{a character denoting 23. Laver les mains avant de manger?}
#' \item{hwwater}{a character denoting 24a. Est-ce qu'il y a de l'eau?}
#' \item{hwsoap}{a character denoting 24b. Est-ce qu'il y a du savon?}
#' \item{treatwater}{a character denoting Traitez vous l'eau?}
#' \item{tptreatwater}{a character denoting Type de traitement}
#' \item{tptreatwaterother}{a character denoting Autre traitment}
#' \item{nottreatwhy}{a character denoting Si non, pourquoi}
#' \item{defecriver}{a character denoting Riviere?.1}
#' \item{defecair}{a character denoting L'air libre?}
#' \item{defeclatrine}{a character denoting Latrine?}
#' \item{numlatrine}{a character denoting 26-Combin de personnes qui utilise le latrine?}
#' \item{dtmedical}{a character denoting Date of Medical Assessment}
#' \item{hpid}{a character denoting 1. Numero de Clinique/Hopital}
#' \item{outreachscreened}{a character denoting Outreach screening? [Y/N/U]}
#' \item{evaljaundice}{a character denoting 7. Deja Evalué pour la Jaunisse?}
#' \item{jaundiceid}{a character denoting Si Oui, Numero de Cas?}
#' \item{referredbychw}{a character denoting 8. Référé par l’Agent Communautaire?}
#' \item{jaundiceid2}{a character denoting Si Oui, Numero de Cas?.1}
#' \item{medjaundice}{a character denoting 9. Le patient a la jaunisse?}
#' \item{dtjaundice}{a character denoting 10. Date de début de la jaunisse ?}
#' \item{medfever}{a character denoting 11a. Fievre?}
#' \item{mednausea}{a character denoting 11b. Nausées/Anorexie }
#' \item{medvomit}{a character denoting 11c. Vomissements?}
#' \item{medepigastric}{a character denoting 11d. Douleur epigastrique?}
#' \item{meditching}{a character denoting 11e. Démangeaisons ?}
#' \item{medheadache}{a character denoting 11f. Mal de tete?}
#' \item{medarthralgia}{a character denoting 11g. Arthralgies?}
#' \item{meddiar}{a character denoting 11h. Diarrhées}
#' \item{medbleeding}{a character denoting 11i. Saignements?}
#' \item{medfev38}{a character denoting 12. Fièvre = 38.0°C?}
#' \item{medmental}{a character denoting 13. Etat Mental?}
#' \item{medother}{a character denoting Other symptoms (free text)}
#' \item{medothhhajs}{a character denoting 14. Autre avec jaunisse dans le menage?}
#' \item{medothhhajsnum}{a character denoting 14a. Combien?}
#' \item{othercaseid}{a character denoting 14b. Numero de cas}
#' \item{medpreg}{a character denoting 15. Enceinte?}
#' \item{medpregtri}{a character denoting 15a. Trimestre? [1/2/3]}
#' \item{medpp}{a character denoting 16. Post-Partum?}
#' \item{medppdays}{a character denoting 16a. Si postpartum, combien de jours?}
#' \item{medppoutcome}{a character denoting 16b. Resultat de la grossesse?}
#' \item{matdeathdt}{a character denoting Si décès maternelle, date de décès}
#' \item{medppga}{a character denoting Gestation weeks of delivery}
#' \item{medblood}{a character denoting 17. Prelevements de sang?}
#' \item{medblooddt}{a character denoting Si oui, date de prelevement}
#' \item{medmalrdt}{a character denoting 18a. Malaria RDT}
#' \item{medhepb}{a character denoting 18b. Hep B}
#' \item{medhepc}{a character denoting 18c. Hep C}
#' \item{medhevrdt}{a character denoting 18d. Hep E RDT}
#' \item{medhevelisa}{a character denoting Hep E confiramtion ELISA [MOH / MSF]}
#' \item{hevrecent}{a character denoting Confirmed 'recent' case based on ELISA [enter 1 if IgM+]}
#' \item{hevpcr}{a character denoting Hep E confirmation PCR [MOH/MSF]}
#' \item{hevgenotest}{a character denoting Genotype peformed? [Y/N/unk]}
#' \item{hevgenotype}{a character denoting If yes, what type?}
#' \item{medhavelisa}{a character denoting Hep A ELISA}
#' \item{medhbvelisa}{a character denoting Hep Bc ELISA}
#' \item{medhcvelisa}{a character denoting Hep C ELISA}
#' \item{arbovpcr}{a character denoting ARBO PCR (YF, Rift, Chik, DEN, WN, ZIK)}
#' \item{yf}{a character denoting Yellow Fever Elisa}
#' \item{zika}{a character denoting Zika Elisa}
#' \item{dengue}{a character denoting Dengue Elisa}
#' \item{wnv}{a character denoting West Nile Elisa}
#' \item{rvf}{a character denoting Rift Valley Elisa}
#' \item{chik}{a character denoting Chikungunya Elisa}
#' \item{hospitalised}{a character denoting 19. Hospitalised?}
#' \item{dthospitalisation}{a character denoting Date de hopitalisation?}
#' \item{outcomehp}{a character denoting 20. Resultat Clinique}
#' \item{dtdeath}{a character denoting (Si décès la date de décès)}
#' \item{epiwajs}{a character denoting EpiWeek Jaundice Onset}
#' \item{epiwhevelisa}{a character denoting EpiWeek Confirmed (IgM+)}
#' \item{epiwoutreach}{a character denoting EpiWeek FIRST Assessment (Community or Medical)}
#' \item{epiwmed}{a character denoting EpiWeek MEDICAL Assessment}
#' \item{epiwepiquest}{a character denoting EpiWeek Assessment QUESTIONNAIRE}
#' \item{epiwhp}{a character denoting EpiWeek HOSPITALIZATION}
#' \item{epiwdeath}{a character denoting EpiWeek DEATH}
#' \item{caseclass}{a character denoting Suspected/Confirmed/Not A Case (based on HEV RDT)}
#' \item{Longitude}{a numeric denoting Jittered GPS coordinates}
#' \item{Latitude}{a numeric denoting Jittered GPS coordinates}
#' }
#'
#' @docType data
Expand Down
91 changes: 91 additions & 0 deletions R/create_desc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#' @title Create Description of Variables for Roxygen Documentation
#'
#' @description
#' This function uses a data frame (in a data dictionary format) to create a the
#' description section of 'format' for data set documentation with the Roxygen
#' package.
#'
#' @param dictionary A data frame listing at minimum the variable name, variable
#' type and a description column.
#' @param variable_name A column listing the names of variables
#' @param type A column listing the type of variable
#' @param description A column explaining what the variable contains
#' @param copy_to_clipboard if `TRUE` (default), the description text will be
#' copied to the user's clipboard with [clipr::write_clip()]. If `FALSE`, it
#' will be printed to the user's console.
#'
#' @importFrom clipr write_clip
#'
#' @return A character string which can be pasted in to the documentation file
#'
#' @examples
#' \dontrun{
#' # Create description text for ...
#' create_desc(data_dict, "variable_name", "type", "description")
#' }
#'
#' @export

create_desc <- function(dictionary, variable_name, type = NULL, description,
copy_to_clipboard = TRUE) {

# Check if dictionary is a data frame
if (!is.data.frame(dictionary)) {
stop("The 'dictionary' argument must be a data frame.")
}

# Check if required columns are present in the dictionary
required_columns <- c(variable_name, description)
if (!all(required_columns %in% colnames(dictionary))) {
missing_cols <- setdiff(required_columns, colnames(dictionary))
stop("The 'dictionary' data frame is missing the following required columns: ",
paste(missing_cols, collapse = ", "))
}

# Initialize the describe block with #' at the beginning
describe_block <- "#' \\describe{\n"

# Loop over each row in the dictionary and append formatted variable descriptions
for (i in seq_len(nrow(dictionary))) {
# Get the variable name, type, and description for the current row
var_name <- dictionary[[variable_name]][i]
var_desc <- dictionary[[description]][i]
var_type <- dictionary[[type]][i]

# Check if var_name and var_desc are valid
if (is.na(var_name) || var_name == "") {
warning("Warning: Variable name at row ", i, " is missing or empty.")
next
}

# If no description is provided in the dictionary, set a default message
if (is.na(var_desc) || var_desc == "") {
var_desc <- "No description available"
}

# If no type is provided in the dictionary, format the description accordingly
if (is.na(var_type) || var_type == "" || var_type == "Unknown") {
# Append to the describe block without type information
describe_block <- paste0(describe_block, "#' \\item{", var_name, "}{", var_desc, "}\n")
} else {
# Append to the describe block with type information and "a" before type
describe_block <- paste0(describe_block, "#' \\item{", var_name, "}{a ", var_type, " denoting ", var_desc, "}\n")
}
}

# Close the describe block with #' and append it
describe_block <- paste0(describe_block, "#' }\n")

# add output to clipboard
if (copy_to_clipboard) {
x <- try(clipr::write_clip(describe_block), silent = TRUE)
if (inherits(x, "try-error")) {
if (interactive()) cat(describe_block)
return(invisible())
}
message("Copied to clipboard. Paste the contents to your documentation file")
} else {
cat(describe_block)
}

}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This package stores all Applied Epi data across books, case studies, courses and
- [ ] download/save dataset locally function (based on *tableoftables*)
- [ ] data dictionary for a dataset: start with wrapper for [{datadict}](https://github.com/epicentre-msf/datadict) as demo, then either consider pulling parts to {epidict} or offer them help to get on CRAN.
- if decide to help w/ {datadict} then {epidict} would just become a fake data generator.
- [ ] function which uses a data dict to create the description section in roxygen for a dataset
- [x] function which uses a data dict to create the description section in roxygen for a dataset
- [ ] in phase 2 could build helper functions that create the description file from *tableoftables* (then the user just needs to edit an excel file)
- [ ] unit testing for functions
- could pull some from [{gapminder}](https://github.com/jennybc/gapminder/)
Expand All @@ -38,7 +38,7 @@ This package stores all Applied Epi data across books, case studies, courses and
- probably do the same way [alison horst](https://github.com/lter/lterdatasampler/) does
- readme/pkgdown table (adapted from *tableoftables*) listing available datasets, versions, languages, source, and where used (e.g. case study, course, tutorial, epirhandbook)
- ~~ideally can be fed by list datasets function~~
- pkgdown separated by heading for dictionaries and actual datasets, and cross-links between the two
- ?pkgdown separated by heading for dictionaries and actual datasets, and cross-links between the two
- guideline for contributing datasets similar to [tidytuesday](https://github.com/rfordatascience/tidytuesday/blob/master/.github/pr_instructions.md)
- licensing: while the overall package repo will be GPL3, it is possible that individual datasets will come under a different license (so there needs to be a license section in documentation for each dataset)

Expand Down
34 changes: 34 additions & 0 deletions data-raw/AJS_AmTiman_dictionary.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## code to prepare `AJS_AmTiman` dictionary goes here
# This first creates an AJS_AmTiman_dict.xlsx from AJS_AmTiman.xlsx
# Then it creates creates an .rda format of the AJS_AmTiman_dict.xlsx file

# # Define the path to the Excel file in inst/extdata
# import_path <- system.file("extdata", "AJS_AmTiman.xlsx", package = "appliedepidata")
#
# # Define the path to export the Excel file (Dictionary) to in inst/extdata
# export_path <- file.path("inst", "extdata", "AJS_AmTiman_dict.xlsx")
#
#
# # Read in the Excel file using rio
# AJS_AmTiman <- rio::import(import_path)
#
#
# # create variable list
# survey <- datadict::dict_from_data(AJS_AmTiman)
# # add in a notes column that can then be edited manually to describe variables
# survey$note <- NA
#
# # create list of variable values (choices)
# choices <- datadict::coded_options(survey)
#
# # chuck in list
# data_export <- list(
# survey,
# choices
# )
#
#
# # write to excel sheet
# rio::export(data_export, export_path)
#

Binary file modified data/1tableoftables.xlsx
Binary file not shown.
Binary file modified inst/extdata/AJS_AmTiman.xlsx
Binary file not shown.
Loading

0 comments on commit 2b5a46c

Please sign in to comment.