diff --git a/DESCRIPTION b/DESCRIPTION index 27c085b..685c6ec 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: ohcleandat Type: Package Title: One Health Data Cleaning and Quality Checking Package -Version: 0.2.1 +Version: 0.2.2 Authors@R: c( person("Collin", "Schwantes", email = "schwantes@ecohealthalliance.org", role = c("cre", "aut"), comment = c(ORCID = "0000-0003-4014-4896")), person("Johana", "Teigen", email = "teigen@ecohealthalliance.org", role = "aut", comment = c(ORCID = "0000-0002-6209-2321")), @@ -14,7 +14,7 @@ Description: This package provides useful functions to orchestrate analytics and License: MIT + file LICENSE Encoding: UTF-8 LazyData: true -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Suggests: knitr, rmarkdown @@ -37,6 +37,7 @@ Imports: tibble, tidyr, tidyselect, + utils, validate Remotes: ecohealthalliance/containerTemplateUtils, diff --git a/NAMESPACE b/NAMESPACE index 7a68dfa..c581658 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(combine_logs) export(correct_data) export(create_freetext_log) export(create_questionnaire_log) +export(create_rules_from_template) export(create_translation_log) export(create_validation_log) export(detect_language) diff --git a/R/create_rules_from_template.R b/R/create_rules_from_template.R new file mode 100644 index 0000000..8f2985c --- /dev/null +++ b/R/create_rules_from_template.R @@ -0,0 +1,72 @@ +#' Title +#' +#' @param name String. Name of rule set function e.g. create_rules_my_dataset +#' @param dir String. Name of directory where file should be created. If it +#' doesnt exist, a folder will be created. +#' @param open Logical. Should the file be opened? +#' @param showWarnings Logical. Should dir.create show warnings? +#' +#' @return String. File path of newly created file +#' @export create_rules_from_template +#' +#' @examples +#' \dontrun{ +#' # create a ruleset and immediately open it +#' create_rules_from_template(name = "create_rules_field_data") +#' # create a ruleset and don't open it +#' create_rules_from_template(name = "create_rules_lab_data", open = FALSE) +#' # create a ruleset and store it in a different folder +#' create_rules_from_template(name = "create_rules_lab_data", +#' dir = "/path/to/rulesets" open = FALSE) +#' } +create_rules_from_template <- function(name, dir = "/R", open = TRUE, showWarnings = FALSE){ + + dir.create(here::here("/R"),showWarnings = showWarnings, recursive = TRUE) + + template_text <- sprintf('%s <- function(){ + ## each rule should be named after the column its validating + rule1 <- validator( + some_column = !is.na(some_column) + ) + + ## make sure descriptions are concise and interpretable + description(rule1) <- rep("Some description",length(rule1)) + . + . + . + ruleN <- validator( + TEST_NO = field_format(TEST_NO, "\\d+" , type="regex") + ) + description(ruleN) <- rep("Some description",length(ruleN)) + + out <- list( + rule1 = rule1, + . + . + . + ruleN = ruleN + ) + + return(out) + }',name) + + + file_name <- sprintf("%s.R",name) + file_path <- paste(dir,file_name,sep = "/") + cat(template_text, file = file_path) + if(open){ + utils::file.edit(file_path) + } + + + return(file_path) + + status <- file.exists(here::here(file_path)) + + if(!status){ + rlang::abort(sprintf("%s not created",file_path)) + } + + return(file_path) + +} diff --git a/man/create_rules_from_template.Rd b/man/create_rules_from_template.Rd new file mode 100644 index 0000000..59733fa --- /dev/null +++ b/man/create_rules_from_template.Rd @@ -0,0 +1,35 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/create_rules_from_template.R +\name{create_rules_from_template} +\alias{create_rules_from_template} +\title{Title} +\usage{ +create_rules_from_template(name, dir = "/R", open = TRUE, showWarnings = FALSE) +} +\arguments{ +\item{name}{String. Name of rule set function e.g. create_rules_my_dataset} + +\item{dir}{String. Name of directory where file should be created. If it +doesnt exist, a folder will be created.} + +\item{open}{Logical. Should the file be opened?} + +\item{showWarnings}{Logical. Should dir.create show warnings?} +} +\value{ +String. File path of newly created file +} +\description{ +Title +} +\examples{ +\dontrun{ + # create a ruleset and immediately open it + create_rules_from_template(name = "create_rules_field_data") + # create a ruleset and don't open it + create_rules_from_template(name = "create_rules_lab_data", open = FALSE) + # create a ruleset and store it in a different folder + create_rules_from_template(name = "create_rules_lab_data", + dir = "/path/to/rulesets" open = FALSE) + } +}