Skip to content

Commit

Permalink
Replace tags with labels
Browse files Browse the repository at this point in the history
The `labels()` is reexported from `datatagr`. Reexport
is done because the labels function is valuable to the
end user of linelist as well.
  • Loading branch information
chartgerink committed Oct 9, 2024
1 parent beab162 commit 211c01f
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 103 deletions.
2 changes: 1 addition & 1 deletion R/has_tag.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ has_tag <- function(
) {

dat <- tidyselect::peek_data(fn = "has_tag")
dat_tags <- tags(dat)
dat_tags <- labels(dat)

cols_to_extract <- dat_tags[names(dat_tags) %in% tags]

Expand Down
3 changes: 3 additions & 0 deletions R/labels.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#' @importFrom datatagr labels
#' @export
datatagr::labels
6 changes: 3 additions & 3 deletions R/linelist-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#'
#' * [set_tags()]: to change or add tagged variables in a `linelist`
#'
#' * [tags()]: to get the list of tags of a `linelist`
#' * [labels()]: to get the list of tags of a `linelist`
#'
#' * [labels_df()]: to get a `data.frame` of all tagged variables
#'
Expand Down Expand Up @@ -62,7 +62,7 @@
#' x
#'
#' ## check tagged variables
#' tags(x)
#' labels(x)
#'
#' ## robust renaming
#' names(x)[1] <- "identifier"
Expand Down Expand Up @@ -108,7 +108,7 @@
#' select(has_tag(c("gender", "age")))
#'
#' x %>%
#' tags()
#' labels()
#'
#' x %>%
#' select(starts_with("date"))
Expand Down
4 changes: 2 additions & 2 deletions R/make_linelist.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#' * An overview of the [linelist] package
#' * [tags_names()]: for a list of known tag names
#' * [tags_types()]: for the associated accepted types/classes
#' * [tags()]: for a list of tagged variables in a `linelist`
#' * [labels()]: for a list of tagged variables in a `linelist`
#' * [set_tags()]: for modifying tags
#' * [labels_df()]: for selecting variables by tags
#'
Expand Down Expand Up @@ -94,7 +94,7 @@
#' head(x)
#'
#' ## check tags
#' tags(x)
#' labels(x)
#'
#' ## Tags can also be passed as a list with the splice operator (!!!)
#' my_tags <- list(
Expand Down
6 changes: 3 additions & 3 deletions R/names.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
#'
#' ## see results: tags have been updated
#' head(x)
#' tags(x)
#' labels(x)
#'
#' # This also works with using `dplyr::rename()` because it uses names<-()
#' # under hood
#' if (require(dplyr)) {
#' x <- x %>%
#' rename(case_id= case_label)
#' head(x)
#' tags(x)
#' labels(x)
#' }
#' }
`names<-.linelist` <- function(x, value) {
Expand All @@ -66,7 +66,7 @@
}

# Step 2
out_tags <- tags(x, TRUE)
out_tags <- labels(x, TRUE)
for (i in seq_along(out_tags)) {
if (!is.null(out_tags[[i]])) {
idx <- match(out_tags[[i]], old_names)
Expand Down
2 changes: 1 addition & 1 deletion R/print.linelist.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
print.linelist <- function(x, ...) {
cat("\n// linelist object\n")
print(datatagr::drop_datatagr(x, remove_labels = TRUE))
tags_txt <- paste(names(tags(x)), unlist(tags(x)), sep = ":", collapse = ", ")
tags_txt <- paste(names(labels(x)), unlist(labels(x)), sep = ":", collapse = ", ")
if (tags_txt == "") {
tags_txt <- "[no tagged variable]"
}
Expand Down
2 changes: 1 addition & 1 deletion R/prune_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ prune_tags <- function(x, lost_action = c("error", "warning", "none")) {
lost_action <- match.arg(lost_action)

# do stuff
old_tags <- tags(x, show_null = TRUE)
old_tags <- labels(x, show_null = TRUE)

has_lost_column <- vapply(
old_tags,
Expand Down
4 changes: 2 additions & 2 deletions R/select_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#' @export
#'
#' @seealso
#' * [tags()] for existing tags in a `linelist`
#' * [labels()] for existing tags in a `linelist`
#' * [labels_df()] to get a `data.frame` of all tags
#'
#' @examples
Expand All @@ -39,7 +39,7 @@
#' head(x)
#'
#' ## check tagged variables
#' tags(x)
#' labels(x)
#'
#' # DEPRECATED!
#' select_tags(x, "gender", "age")
Expand Down
12 changes: 6 additions & 6 deletions R/set_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@
#' if (require(outbreaks)) {
#' ## create a linelist
#' x <- make_linelist(measles_hagelloch_1861, date_onset = "date_of_rash")
#' tags(x)
#' labels(x)
#'
#' ## add new tags and fix an existing one
#' x <- set_tags(x,
#' age = "age",
#' gender = "gender",
#' date_onset = "date_of_prodrome"
#' )
#' tags(x)
#' labels(x)
#'
#' ## add non-default tags using allow_extra
#' x <- set_tags(x, severe = "complications", allow_extra = TRUE)
#' tags(x)
#' labels(x)
#'
#' ## remove tags by setting them to NULL
#' old_tags <- tags(x)
#' old_tags <- labels(x)
#' x <- set_tags(x, age = NULL, gender = NULL)
#' tags(x)
#' labels(x)
#'
#' ## setting tags providing a list (used to restore old tags here)
#' x <- set_tags(x, !!!old_tags)
#' tags(x)
#' labels(x)
#' }
#'
set_tags <- function(x, ..., allow_extra = FALSE) {
Expand Down
8 changes: 4 additions & 4 deletions R/square_bracket.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
}

# Case 2
old_tags <- tags(x, show_null = TRUE)
old_tags <- labels(x, show_null = TRUE)
out <- restore_tags(out, old_tags, lost_action)

out
Expand All @@ -107,7 +107,7 @@
`[<-.linelist` <- function(x, i, j, value) {
lost_action <- get_lost_tags_action()
out <- NextMethod()
old_tags <- tags(x, show_null = TRUE)
old_tags <- labels(x, show_null = TRUE)
out <- restore_tags(out, old_tags, lost_action)
out
}
Expand All @@ -119,7 +119,7 @@
`[[<-.linelist` <- function(x, i, j, value) {
lost_action <- get_lost_tags_action()
out <- NextMethod()
old_tags <- tags(x, show_null = TRUE)
old_tags <- labels(x, show_null = TRUE)
out <- restore_tags(out, old_tags, lost_action)
out
}
Expand All @@ -130,7 +130,7 @@
`$<-.linelist` <- function(x, name, value) {
lost_action <- get_lost_tags_action()
out <- NextMethod()
old_tags <- tags(x, show_null = TRUE)
old_tags <- labels(x, show_null = TRUE)
out <- restore_tags(out, old_tags, lost_action)
out
}
40 changes: 0 additions & 40 deletions R/tags.R

This file was deleted.

4 changes: 2 additions & 2 deletions R/validate_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#' }
validate_tags <- function(x, allow_extra = FALSE) {
checkmate::assert_class(x, "linelist")
x_tags <- tags(x, show_null = TRUE)
x_tags <- labels(x, show_null = TRUE)

stopifnot(
"`x` has no tags attribute" = !is.null(x_tags)
Expand Down Expand Up @@ -76,7 +76,7 @@ validate_tags <- function(x, allow_extra = FALSE) {
}

# check that tagged variables exist
x_tags_vec <- unlist(tags(x))
x_tags_vec <- unlist(labels(x))
var_exists <- x_tags_vec %in% names(x)
if (!all(var_exists)) {
missing_var <- x_tags_vec[!var_exists]
Expand Down
22 changes: 11 additions & 11 deletions tests/testthat/test-compat-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ test_that("Compatibility with dplyr::filter()", {
# nolint end: expect_named_linter

expect_identical(
tags(dplyr::filter(x, dist > mean(dist))),
tags(x)
labels(dplyr::filter(x, dist > mean(dist))),
labels(x)
)

})
Expand Down Expand Up @@ -75,8 +75,8 @@ test_that("Compatibility with dplyr::mutate(.keep)", {
x %>%
dplyr::mutate(speed = as.integer(speed)) %>%
expect_s3_class("linelist") %>%
tags() %>%
expect_identical(tags(x))
labels() %>%
expect_identical(labels(x))

})

Expand Down Expand Up @@ -105,7 +105,7 @@ test_that("Compatibility with dplyr::relocate()", {
test_that("Compatibility with dplyr::rename()", {

expect_identical(
tags(dplyr::rename(x, toto = dist)),
labels(dplyr::rename(x, toto = dist)),
list(date_onset = "toto", date_outcome = "speed")
)

Expand All @@ -124,8 +124,8 @@ test_that("Compatibility with dplyr::rename()", {
test_that("Compatibility with dplyr::rename_with()", {

expect_identical(
tags(dplyr::rename_with(x, toupper)),
lapply(tags(x), toupper)
labels(dplyr::rename_with(x, toupper)),
lapply(labels(x), toupper)
)

# Identity
Expand All @@ -145,15 +145,15 @@ test_that("Compatibility with dplyr::select()", {
x %>%
dplyr::select("dist") %>%
expect_s3_class("linelist") %>%
tags() %>%
labels() %>%
expect_identical(list(date_onset = "dist")) %>%
expect_snapshot_warning()

# Even when renames happen
x %>%
dplyr::select(dist, vitesse = speed) %>%
expect_s3_class("linelist") %>%
tags() %>%
labels() %>%
expect_identical(list(date_onset = "dist", date_outcome = "vitesse"))

})
Expand All @@ -165,8 +165,8 @@ test_that("Compatibility with dplyr::bind_rows()", {
rbound_x <- dplyr::bind_rows(x, x)

expect_identical(
tags(x),
tags(rbound_x)
labels(x),
labels(rbound_x)
)

expect_s3_class(
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-make_linelist.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ test_that("tests for make_linelist", {
)

# test functionalities
expect_identical(tags_defaults(), tags(make_linelist(cars), TRUE))
expect_identical(tags_defaults(), labels(make_linelist(cars), TRUE))

x <- make_linelist(cars, date_onset = "dist", date_outcome = "speed")
expect_identical(tags(x)$date_onset, "dist")
expect_identical(tags(x)$date_outcome, "speed")
expect_null(tags(x)$outcome)
expect_null(tags(x)$date_reporting)
expect_identical(labels(x)$date_onset, "dist")
expect_identical(labels(x)$date_outcome, "speed")
expect_null(labels(x)$outcome)
expect_null(labels(x)$date_reporting)

x <- make_linelist(cars, foo = "speed", bar = "dist", allow_extra = TRUE)
expect_identical(
tags(x, TRUE),
labels(x, TRUE),
c(tags_defaults(), foo = "speed", bar = "dist")
)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-names.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test_that("tests for the names<- operator", {
# functionalities
names(x) <- c("titi", "toto")
expect_named(x, c("titi", "toto"))
expect_identical(tags(x), list(id = "titi", age = "toto"))
expect_identical(labels(x), list(id = "titi", age = "toto"))
expect_s3_class(x, old_class)
names(x) <- old_names
expect_identical(x, old_x)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-prune_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test_that("tests for prune_tags", {

# Check functionality
y <- prune_tags(x, "none")
expect_identical(tags_defaults(), tags(y, TRUE))
expect_identical(tags_defaults(), labels(y, TRUE))
expect_s3_class(y, "linelist")
})

Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-restore_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ test_that("tests for restore_tags", {
" date_onset:dist, age:speed",
sep = "\n"
)
expect_error(restore_tags(z, tags(x), "error"), msg)
expect_warning(restore_tags(z, tags(x), "warning"), msg)
expect_error(restore_tags(z, labels(x), "error"), msg)
expect_warning(restore_tags(z, labels(x), "warning"), msg)

# Check functionality
expect_identical(x, restore_tags(x, tags(x)))
expect_identical(x, restore_tags(y, tags(x)))
expect_identical(x, restore_tags(x, labels(x)))
expect_identical(x, restore_tags(y, labels(x)))
})
4 changes: 2 additions & 2 deletions tests/testthat/test-set_tags.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ test_that("tests for set_tags()", {
# Check functionality
expect_identical(x, set_tags(x))
x <- set_tags(x, date_reporting = "speed")
expect_identical(tags(x)$date_reporting, "speed")
expect_identical(tags(x)$date_onset, "dist")
expect_identical(labels(x)$date_reporting, "speed")
expect_identical(labels(x)$date_onset, "dist")

x <- set_tags(x, id = "speed", date_outcome = "dist")
y <- set_tags(x, !!!list(id = "speed", date_outcome = "dist"))
Expand Down
Loading

0 comments on commit 211c01f

Please sign in to comment.