Skip to content

Commit ca68c00

Browse files
committed
test: label with a function
1 parent 904844b commit ca68c00

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

R/labels.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ apply_labels = function(.data, ..., fn, warn_missing=FALSE) {
281281
if(!missing(fn)){
282282
check_dots_empty()
283283
fn = as_function(fn)
284-
rtn = .data %>%
285-
mutate(across(everything(),
286-
~set_label(.x, fn(get_label(.x)))))
284+
rtn = set_label(.data, value=fn)
287285
} else {
288286
args = lst(...)
289287
unknowns = setdiff(names(args), names(.data))

tests/testthat/test-labels.R

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,35 @@ test_that("Labelling nested lists (set)", {
8383
expect_setequal(get_label(xx, recursive=TRUE), "not foobar at all")
8484
})
8585

86+
test_that("With a function", {
87+
#with labels
88+
l = mtcars2 %>% apply_labels(fn=toupper) %>% get_label()
89+
l2 = mtcars2 %>% set_label(value=toupper) %>% get_label()
90+
expect_identical(l, toupper(l))
91+
expect_identical(l, l2)
92+
93+
#with names
94+
l = mtcars %>% apply_labels(fn=toupper) %>% get_label()
95+
l2 = mtcars %>% set_label(value=toupper) %>% get_label()
96+
expect_identical(l, toupper(l))
97+
expect_identical(l, l2)
98+
})
99+
86100
test_that("Copying labels", {
87101
x = mtcars2 %>%
88102
mutate(mpg2=as.numeric(mpg)+1,
89-
mpg3=copy_label_from(mpg2, mpg))
103+
mpg3=copy_label_from(mpg2, mpg)) %>%
104+
apply_labels(am="foobar")
90105
expect_null(get_label(x$mpg2))
91106
expect_equal(get_label(x$mpg3), "Miles/(US) gallon")
107+
108+
y = mtcars2 %>%
109+
copy_label_from(x)
110+
expect_equal(get_label(y$am), "foobar")
92111
})
93112

113+
114+
94115
test_that("Removing labels", {
95116
x = mtcars2$mpg
96117
x2 = remove_label(mtcars2$mpg)

0 commit comments

Comments
 (0)