Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pawelru opened this issue Feb 27, 2024 · 1 comment · Fixed by #263
Closed

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

pawelru opened this issue Feb 27, 2024 · 1 comment · Fixed by #263
Assignees
Labels
core sme Tracks changes for the sme board

Comments

@pawelru
Copy link
Collaborator

pawelru commented Feb 27, 2024

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 -> insightsengineering/teal.data#301

Please make a good test case against it.

@chlebowa
Copy link

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

@pawelru pawelru added sme Tracks changes for the sme board core labels Feb 27, 2024
@Melkiades Melkiades self-assigned this Feb 27, 2024
Melkiades added a commit that referenced this issue Mar 4, 2024
Fixes #262 

``` r
library(formatters)
    labels <- letters[1:5]
    var_labels(iris) <- labels
    
    old_iris <- iris
    var_labels(iris) <- var_labels(iris)
    testthat::expect_identical(old_iris, iris)
    var_labels(iris)
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#>          "a"          "b"          "c"          "d"          "e"
    
    # teal.data
    labels <- letters[1:5]
    var_labels(iris) <- labels
    
    old_iris <- iris
    teal.data::col_labels(iris) <- teal.data::col_labels(iris)
    testthat::expect_identical(old_iris, iris)
    teal.data::col_labels(iris)
#> Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#>          "a"          "b"          "c"          "d"          "e"
```

<sup>Created on 2024-02-27 with [reprex
v2.1.0](https://reprex.tidyverse.org)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core sme Tracks changes for the sme board
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants