From b3be2c4c24d89f38decaa1637c9ead07f7663030 Mon Sep 17 00:00:00 2001 From: Joseph Larmarange Date: Tue, 19 Mar 2024 13:46:56 +0100 Subject: [PATCH] error when applying value labels to Date (#157) fix #156 --- R/val_labels.R | 4 ++-- tests/testthat/test-labelled.r | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/R/val_labels.R b/R/val_labels.R index 2892236..2e50cb7 100644 --- a/R/val_labels.R +++ b/R/val_labels.R @@ -70,8 +70,8 @@ val_labels.data.frame <- function(x, prefixed = FALSE) { null_action = c("unclass", "labelled"), value) { null_action <- match.arg(null_action) - if (null_action == "labelled") { - x <- labelled(x, value, label = val_label(x)) + if (!is.null(value) || null_action == "labelled") { + x <- labelled(x, value, label = var_label(x)) } # otherwise do nothing x diff --git a/tests/testthat/test-labelled.r b/tests/testthat/test-labelled.r index 776079c..c1ca879 100644 --- a/tests/testthat/test-labelled.r +++ b/tests/testthat/test-labelled.r @@ -334,6 +334,10 @@ test_that(" 'val_label<-' works properly", { expect_error(val_label(df, 2:3) <- "a") sub_df <- df[, -match("ch", names(df))] + + v <- as.Date("2023-01-01") + l <- as.Date(c("The first day of 2023" = "2023-01-01")) + expect_error(val_labels(v) <- l) }) test_that(" 'val_label<-.data.frame' works properly", {