Skip to content

Commit

Permalink
Remove linelist default values from tags_types
Browse files Browse the repository at this point in the history
  • Loading branch information
chartgerink committed Aug 1, 2024
1 parent 01c0ff2 commit 6a92ed0
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 124 deletions.
48 changes: 3 additions & 45 deletions R/make_datatagr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 11 in R/make_datatagr.R

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=R/make_datatagr.R,line=11,col=71,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' `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
Expand All @@ -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.
Expand Down
31 changes: 2 additions & 29 deletions R/tags_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
35 changes: 3 additions & 32 deletions man/make_datatagr.Rd

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

7 changes: 3 additions & 4 deletions man/set_tags.Rd

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

7 changes: 3 additions & 4 deletions man/tags_types.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/validate_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'


4 changes: 0 additions & 4 deletions tests/testthat/test-tags_types.R
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-validate_datatagr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
)

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-validate_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
)
)

Expand All @@ -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"))
)
})

Expand All @@ -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
Expand Down

0 comments on commit 6a92ed0

Please sign in to comment.