diff --git a/DESCRIPTION b/DESCRIPTION index cb4e32a..d6345a9 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.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")), @@ -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 diff --git a/R/create_rules_from_template.R b/R/create_rules_from_template.R index 6ab64fb..26bb4f7 100644 --- a/R/create_rules_from_template.R +++ b/R/create_rules_from_template.R @@ -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 @@ -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 @@ -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){ diff --git a/man/create_rules_from_template.Rd b/man/create_rules_from_template.Rd index adf7dab..c3f0be3 100644 --- a/man/create_rules_from_template.Rd +++ b/man/create_rules_from_template.Rd @@ -2,9 +2,15 @@ % Please edit documentation in R/create_rules_from_template.R \name{create_rules_from_template} \alias{create_rules_from_template} -\title{Title} +\title{Create a "rules" file from a template} \usage{ -create_rules_from_template(name, dir = "/R", open = TRUE, showWarnings = FALSE) +create_rules_from_template( + name, + dir = "R", + open = TRUE, + showWarnings = FALSE, + overwrite_file = FALSE +) } \arguments{ \item{name}{String. Name of rule set function e.g. create_rules_my_dataset} @@ -15,12 +21,14 @@ doesnt exist, a folder will be created.} \item{open}{Logical. Should the file be opened?} \item{showWarnings}{Logical. Should dir.create show warnings?} + +\item{overwrite_file}{Logical. Should a rules file with the same name be overwritten?} } \value{ String. File path of newly created file } \description{ -Title +Creates a rules file from a template to show general structure of the rule file. } \examples{ \dontrun{