Skip to content

Commit

Permalink
Merge pull request #39 from ecohealthalliance/hotfix/create_rules_fro…
Browse files Browse the repository at this point in the history
…m_template

Hotfix/create rules from template
  • Loading branch information
collinschwantes authored Jul 26, 2024
2 parents 8576a96 + 1f695b8 commit 9196f68
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
4 changes: 2 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.5
Version: 0.2.6
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.3.1
RoxygenNote: 7.3.2
Suggests:
knitr,
rmarkdown
Expand Down
27 changes: 19 additions & 8 deletions R/create_rules_from_template.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#' Title
#' Create a "rules" file from a template
#'
#' Creates a rules file from a template to show general structure of the rule file.
#'
#'
#' @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?
#' @param overwrite_file Logical. Should a rules file with the same name be overwritten?
#'
#' @return String. File path of newly created file
#' @export create_rules_from_template
#'
#'
#'
#' @examples
#' \dontrun{
#' # create a ruleset and immediately open it
Expand All @@ -21,9 +23,11 @@
#' 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){
create_rules_from_template <- function(name, dir = "R", open = TRUE,
showWarnings = FALSE,
overwrite_file = FALSE){

dir.create(here::here("/R"),showWarnings = showWarnings, recursive = TRUE)
dir.create(here::here(dir),showWarnings = showWarnings, recursive = TRUE)

template_text <- sprintf('%s <- function(){
## each rule should be named after the column its validating
Expand Down Expand Up @@ -55,14 +59,21 @@ create_rules_from_template <- function(name, dir = "/R", open = TRUE, showWarnin

file_name <- sprintf("%s.R",name)
file_path <- paste(dir,file_name,sep = "/")

status_prelim <- file.exists(here::here(file_path))

if(status_prelim & !overwrite_file){
rlang::abort("File already exists. Set overwrite_file = TRUE to
create the file.")
}


file.create(file_path,showWarnings = showWarnings)
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){
Expand Down
14 changes: 11 additions & 3 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 9196f68

Please sign in to comment.