diff --git a/R/formatters_var_labels.R b/R/formatters_var_labels.R index 55cb1fcb9..02e0d2cc6 100644 --- a/R/formatters_var_labels.R +++ b/R/formatters_var_labels.R @@ -43,19 +43,24 @@ col_labels <- function(x, fill = FALSE) { return(character(0L)) } - labels <- lapply(x, function(x) as.vector(attr(x, "label")[[1]])) - - nulls <- vapply(labels, is.null, logical(1L)) - if (any(nulls)) { - labels[nulls] <- - if (fill) { - colnames(x)[nulls] + vapply( + colnames(x), + function(colname) { + label <- as.vector(attr(x[[colname]], "label")) + checkmate::assert_string(label, .var.name = sprintf("'%s' column label", colname), null.ok = TRUE) + if (is.null(label)) { + if (fill) { + colname + } else { + NA_character_ + } } else { - NA_character_ + label } - } - - unlist(labels) + }, + character(1L), + USE.NAMES = TRUE + ) } #' @rdname col_labels diff --git a/tests/testthat/test-formatters_var_labels.R b/tests/testthat/test-formatters_var_labels.R index e00be84f0..d63636eb0 100644 --- a/tests/testthat/test-formatters_var_labels.R +++ b/tests/testthat/test-formatters_var_labels.R @@ -25,7 +25,7 @@ testthat::test_that("col_labels works with labels having additional attributes ( x <- iris attr(x$Species, "label") <- structure("Label for Species", names = "blah", foo = "bar") testthat::expect_identical(col_labels(x)[["Species"]], "Label for Species") -)} +}) testthat::test_that("col_labels returns only 'names' attribute and ignores all the rest", { x <- iris @@ -33,6 +33,24 @@ testthat::test_that("col_labels returns only 'names' attribute and ignores all t testthat::expect_identical(attributes(col_labels(x)["Species"]), list(names = "Species")) }) +testthat::test_that("col_labels throws if label is not a character", { + x <- iris + attr(x$Species, "label") <- structure(1, names = "blah", foo = "bar") + testthat::expect_error( + col_labels(x), + "Assertion on ''Species' column label' failed" + ) +}) + +testthat::test_that("col_labels throws if label is not a character of length 1", { + x <- iris + attr(x$Species, "label") <- structure(c("a", "b"), names = "blah", foo = "bar") + testthat::expect_error( + col_labels(x), + "Assertion on ''Species' column label' failed" + ) +}) + # col_labels ---- testthat::test_that("col_labels<- value accepts character vector", { iris_df <- utils::head(iris, 2)