Skip to content

Commit

Permalink
Merge pull request #22 from ecohealthalliance/feature/create_rules_fr…
Browse files Browse the repository at this point in the history
…om_template

Feature/create rules from template
  • Loading branch information
collinschwantes committed May 10, 2024
2 parents 3df3bc3 + 990c3ed commit 23810e4
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand All @@ -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
Expand All @@ -37,6 +37,7 @@ Imports:
tibble,
tidyr,
tidyselect,
utils,
validate
Remotes:
ecohealthalliance/containerTemplateUtils,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
72 changes: 72 additions & 0 deletions R/create_rules_from_template.R
Original file line number Diff line number Diff line change
@@ -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)

}
35 changes: 35 additions & 0 deletions man/create_rules_from_template.Rd

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

0 comments on commit 23810e4

Please sign in to comment.