From 6a92ed039447ae30bf8fa314aa359e11d1038dab Mon Sep 17 00:00:00 2001 From: Chris Hartgerink Date: Thu, 1 Aug 2024 14:41:51 +0200 Subject: [PATCH] Remove linelist default values from `tags_types` --- R/make_datatagr.R | 48 ++----------------------- R/tags_types.R | 31 ++-------------- man/make_datatagr.Rd | 35 ++---------------- man/set_tags.Rd | 7 ++-- man/tags_types.Rd | 7 ++-- tests/testthat/_snaps/validate_types.md | 2 +- tests/testthat/test-tags_types.R | 4 --- tests/testthat/test-validate_datatagr.R | 5 +-- tests/testthat/test-validate_types.R | 6 ++-- 9 files changed, 21 insertions(+), 124 deletions(-) diff --git a/R/make_datatagr.R b/R/make_datatagr.R index 4ef2710..a1c1153 100644 --- a/R/make_datatagr.R +++ b/R/make_datatagr.R @@ -8,12 +8,11 @@ #' @param x a `data.frame` or a `tibble` #' #' @param ... <[`dynamic-dots`][rlang::dyn-dots]> A series of tags provided as -#' `tag_name = "column_name"`, where `tag_name` indicates any of the known -#' variables listed in 'Details' and values indicate their name in `x`; see -#' details for a list of known variable types and their expected content +#' `tag_name = "column_name"`. When specifying tags, please also see +#' `tag_defaults` to specify default values. #' #' @param tag_defaults a list of default values for the provided tags. Defaults -#' to `list()` +#' to `list()`, effectively defaulting to NULL values. #' #' @param allow_extra a `logical` indicating if additional data tags not #' currently recognized by `datatagr` should be allowed; if `FALSE`, unknown @@ -27,47 +26,6 @@ #' * [set_tags()]: for modifying tags #' * [tags_df()]: for selecting variables by tags #' -#' @details Known variable types include: -#' -#' * `id`: a unique case identifier as `numeric` or `character` -#' -#' * `date_onset`: date of symptom onset (see below for date formats) -#' -#' * `date_reporting`: date of case notification (see below for date formats) -#' -#' * `date_admission`: date of hospital admission (see below for date formats) -#' -#' * `date_discharge`: date of hospital discharge (see below for date formats) -#' -#' * `date_outcome`: date of disease outcome (see below for date formats) -#' -#' * `date_death`: date of death (see below for date formats) -#' -#' * `gender`: a `factor` or `character` indicating the gender of the patient -#' -#' * `age`: a `numeric` indicating the age of the patient, in years -#' -#' * `location`: a `factor` or `character` indicating the location of the -#' patient -#' -#' * `occupation`: a `factor` or `character` indicating the professional -#' activity of the patient -#' -#' * `hcw`: a `logical` indicating if the patient is a health care worker -#' -#' * `outcome`: a `factor` or `character` indicating the outcome of the disease -#' (death or survival) -#' -#' Dates can be provided in the following formats/types: -#' -#' * `Date` objects (e.g. using `as.Date` on a `character` with a correct date -#' format); this is the recommended format -#' -#' * `POSIXct/POSIXlt` objects (when a finer scale than days is needed) -#' -#' * `numeric` values, typically indicating the number of days since the first -#' case -#' #' @export #' #' @return The function returns a `datatagr` object. diff --git a/R/tags_types.R b/R/tags_types.R index c583e2a..59436fe 100644 --- a/R/tags_types.R +++ b/R/tags_types.R @@ -24,38 +24,11 @@ #' # add new types e.g. to allow genetic sequences using ape's format #' tags_types(sequence = "DNAbin", allow_extra = TRUE) #' -tags_types <- function(..., allow_extra = FALSE) { - defaults <- list( - id = c("numeric", "integer", "character"), - date_onset = date_types, - date_reporting = date_types, - date_admission = date_types, - date_discharge = date_types, - date_outcome = date_types, - date_death = date_types, - gender = category_types, - age = numeric_types, - location = category_types, - occupation = category_types, - hcw = binary_types, - outcome = category_types - ) +tags_types <- function(..., allow_extra = TRUE) { + defaults <- list() new_values <- rlang::list2(...) checkmate::assert_list(new_values, types = "character") modify_defaults(defaults = defaults, x = new_values, strict = !allow_extra) } - - -#' @noRd -date_types <- c("integer", "numeric", "Date", "POSIXct", "POSIXlt") - -#' @noRd -category_types <- c("character", "factor") - -#' @noRd -numeric_types <- c("numeric", "integer") - -#' @noRd -binary_types <- c("logical", "integer", "character", "factor") diff --git a/man/make_datatagr.Rd b/man/make_datatagr.Rd index 9cf6b30..5a82bca 100644 --- a/man/make_datatagr.Rd +++ b/man/make_datatagr.Rd @@ -10,12 +10,11 @@ make_datatagr(x, ..., tag_defaults = list(), allow_extra = TRUE) \item{x}{a \code{data.frame} or a \code{tibble}} \item{...}{<\code{\link[rlang:dyn-dots]{dynamic-dots}}> A series of tags provided as -\code{tag_name = "column_name"}, where \code{tag_name} indicates any of the known -variables listed in 'Details' and values indicate their name in \code{x}; see -details for a list of known variable types and their expected content} +\code{tag_name = "column_name"}. When specifying tags, please also see +\code{tag_defaults} to specify default values.} \item{tag_defaults}{a list of default values for the provided tags. Defaults -to \code{list()}} +to \code{list()}, effectively defaulting to NULL values.} \item{allow_extra}{a \code{logical} indicating if additional data tags not currently recognized by \code{datatagr} should be allowed; if \code{FALSE}, unknown @@ -30,34 +29,6 @@ where data are tagged. The output will seem to be the same \code{data.frame}, bu \code{datatagr}-aware packages will then be able to automatically use tagged fields for further data cleaning and analysis. } -\details{ -Known variable types include: -\itemize{ -\item \code{id}: a unique case identifier as \code{numeric} or \code{character} -\item \code{date_onset}: date of symptom onset (see below for date formats) -\item \code{date_reporting}: date of case notification (see below for date formats) -\item \code{date_admission}: date of hospital admission (see below for date formats) -\item \code{date_discharge}: date of hospital discharge (see below for date formats) -\item \code{date_outcome}: date of disease outcome (see below for date formats) -\item \code{date_death}: date of death (see below for date formats) -\item \code{gender}: a \code{factor} or \code{character} indicating the gender of the patient -\item \code{age}: a \code{numeric} indicating the age of the patient, in years -\item \code{location}: a \code{factor} or \code{character} indicating the location of the -patient -\item \code{occupation}: a \code{factor} or \code{character} indicating the professional -activity of the patient -\item \code{hcw}: a \code{logical} indicating if the patient is a health care worker -\item \code{outcome}: a \code{factor} or \code{character} indicating the outcome of the disease -(death or survival) - -Dates can be provided in the following formats/types: -\item \code{Date} objects (e.g. using \code{as.Date} on a \code{character} with a correct date -format); this is the recommended format -\item \code{POSIXct/POSIXlt} objects (when a finer scale than days is needed) -\item \code{numeric} values, typically indicating the number of days since the first -case -} -} \examples{ x <- make_datatagr(cars, diff --git a/man/set_tags.Rd b/man/set_tags.Rd index 7dd412b..535d270 100644 --- a/man/set_tags.Rd +++ b/man/set_tags.Rd @@ -10,12 +10,11 @@ set_tags(x, ..., tag_defaults = list(), allow_extra = TRUE) \item{x}{a \code{data.frame} or a \code{tibble}} \item{...}{<\code{\link[rlang:dyn-dots]{dynamic-dots}}> A series of tags provided as -\code{tag_name = "column_name"}, where \code{tag_name} indicates any of the known -variables listed in 'Details' and values indicate their name in \code{x}; see -details for a list of known variable types and their expected content} +\code{tag_name = "column_name"}. When specifying tags, please also see +\code{tag_defaults} to specify default values.} \item{tag_defaults}{a list of default values for the provided tags. Defaults -to \code{list()}} +to \code{list()}, effectively defaulting to NULL values.} \item{allow_extra}{a \code{logical} indicating if additional data tags not currently recognized by \code{datatagr} should be allowed; if \code{FALSE}, unknown diff --git a/man/tags_types.Rd b/man/tags_types.Rd index f11dcbc..d407c28 100644 --- a/man/tags_types.Rd +++ b/man/tags_types.Rd @@ -4,13 +4,12 @@ \alias{tags_types} \title{List acceptable variable types for tags} \usage{ -tags_types(..., allow_extra = FALSE) +tags_types(..., allow_extra = TRUE) } \arguments{ \item{...}{<\code{\link[rlang:dyn-dots]{dynamic-dots}}> A series of tags provided as -\code{tag_name = "column_name"}, where \code{tag_name} indicates any of the known -variables listed in 'Details' and values indicate their name in \code{x}; see -details for a list of known variable types and their expected content} +\code{tag_name = "column_name"}. When specifying tags, please also see +\code{tag_defaults} to specify default values.} \item{allow_extra}{a \code{logical} indicating if additional data tags not currently recognized by \code{datatagr} should be allowed; if \code{FALSE}, unknown diff --git a/tests/testthat/_snaps/validate_types.md b/tests/testthat/_snaps/validate_types.md index 8a73db2..2603f7c 100644 --- a/tests/testthat/_snaps/validate_types.md +++ b/tests/testthat/_snaps/validate_types.md @@ -2,6 +2,6 @@ Some tags have the wrong class: - age: Must inherit from class 'factor', but has class 'numeric' - - gender: Must inherit from class 'character'/'factor', but has class 'numeric' + - gender: Must inherit from class 'character', but has class 'numeric' diff --git a/tests/testthat/test-tags_types.R b/tests/testthat/test-tags_types.R index b1b9d74..84be3b5 100644 --- a/tests/testthat/test-tags_types.R +++ b/tests/testthat/test-tags_types.R @@ -1,8 +1,4 @@ test_that("tests for tags_types", { - # Check errors - msg <- "Use only known tags or set `allow_extra = TRUE`" - expect_error(tags_types(toto = "ilestbo"), msg, fixed = TRUE) - # Check functionality x <- tags_types() expect_type(x, "list") diff --git a/tests/testthat/test-validate_datatagr.R b/tests/testthat/test-validate_datatagr.R index cb26b47..69c101b 100644 --- a/tests/testthat/test-validate_datatagr.R +++ b/tests/testthat/test-validate_datatagr.R @@ -4,13 +4,14 @@ test_that("tests for validate_datatagr", { expect_error(validate_datatagr(NULL), msg) x <- make_datatagr(cars, id = "speed", toto = "dist", allow_extra = TRUE) - msg <- "Allowed types for tag `toto` are not documented in `ref_types`." + msg <- "Allowed types for tag `id`, `toto` are not documented in `ref_types`." expect_error(validate_datatagr(x), msg) expect_identical(x, validate_datatagr(x, ref_types = list(id = "numeric", toto = "numeric"))) x <- make_datatagr(cars, gender = "speed") expect_error( - validate_datatagr(x), + validate_datatagr(x, ref_types = tags_types(gender = c('character', + 'factor'))), "- gender: Must inherit from class 'character'/'factor'" ) diff --git a/tests/testthat/test-validate_types.R b/tests/testthat/test-validate_types.R index b2aa0b0..b584f20 100644 --- a/tests/testthat/test-validate_types.R +++ b/tests/testthat/test-validate_types.R @@ -11,7 +11,7 @@ test_that("validate_types() validates types", { expect_silent( expect_identical( x, - validate_types(x) + validate_types(x, ref_types = tags_types(age = 'numeric')) ) ) @@ -24,7 +24,7 @@ test_that("validate_types() validates types", { x <- make_datatagr(cars, age = "speed", gender = "dist") expect_snapshot_error( - validate_types(x, ref_types = tags_types(age = "factor")) + validate_types(x, ref_types = tags_types(age = "factor", gender = "character")) ) }) @@ -33,7 +33,7 @@ test_that("missing ref_type in validate_types()", { x <- make_datatagr(cars, age = "speed", d = "dist", allow_extra = TRUE) expect_error( validate_types(x), - "Allowed types for tag `d` are not documented in `ref_types`." + "Allowed types for tag `age`, `d` are not documented in `ref_types`." ) # Two missing