insightsengineering / formatters

A framework for creating listings of raw data that include specialized formatting, headers, footers, referential footnotes, and pagination.
https://insightsengineering.github.io/formatters/
Other
15 stars 6 forks source link

`var_label<-`: assign atomic value (with no attributes) #262

Closed pawelru closed 5 months ago

pawelru commented 5 months ago

In a simple terms: var_label(x) <- var_label(x) should not modify x if it has non-empty labels for all cols. That's not true right now:

library(formatters)
labels <- letters[1:5]
var_labels(iris) <- labels

old_iris <- iris
var_labels(iris) <- var_labels(iris)

identical(old_iris, iris)
#> [1] FALSE

Created on 2024-02-27 with reprex v2.1.0

Let's analyse why:

library(formatters)

labels <- letters[1:5]
var_labels(iris) <- labels

old_species_label <- attr(iris[["Species"]], "label")

var_labels(iris) <- var_labels(iris)
new_species_label <- attr(iris[["Species"]], "label")

identical(old_species_label, new_species_label)
#> [1] FALSE
old_species_label
#> [1] "e"
new_species_label
#> Species 
#>     "e"

Created on 2024-02-27 with reprex v2.1.0

It seems to me that in assign we save a named vector instead of atomic value. Luckily the getter functionality (i.e. var_label) handling this correctly but there might be other getters not that smart -> https://github.com/insightsengineering/teal.data/pull/301

Please make a good test case against it.

chlebowa commented 5 months ago

See teal.data::col_labels for a rewrite of these functions.