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

as_tibble.data.frame() treats attr()-type attributes inconsistently: sripts "n" but not others. #1573

Closed
krivit opened this issue Apr 2, 2024 · 2 comments · Fixed by #1574

Comments

@krivit
Copy link

krivit commented Apr 2, 2024

as_tibble.data.frame() appears to strip some custom attributes. So far, I've only noticed this for n, but there may be others. I think it should either strip all the non-standard attributes or preserve them all, and document the behaviour either way.

library(tibble)
df <- data.frame(x=1:2, y=3:4)
attr(df, "n") <- 5
attr(df, "m") <- 6

attributes(df) # data.frame has m and n attributes.
#> $names
#> [1] "x" "y"
#> 
#> $class
#> [1] "data.frame"
#> 
#> $row.names
#> [1] 1 2
#> 
#> $n
#> [1] 5
#> 
#> $m
#> [1] 6

attributes(as_tibble(df)) # tibble has m but not n.
#> $class
#> [1] "tbl_df"     "tbl"        "data.frame"
#> 
#> $row.names
#> [1] 1 2
#> 
#> $names
#> [1] "x" "y"
#> 
#> $m
#> [1] 6

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

@qmarcou
Copy link

qmarcou commented Apr 8, 2024

It seems dropping attributes was an ongoing discussion last year (see #769 )
I also just experienced a problem with remnant attributes after conversion to tibble that breaks some automated testing utilities. For instance converting back and forth a tibble to data.table breaks equality using testthat:

test_df = tibble(a = c(1,2,3))
converted_df = a %>% data.table::as.data.table() %>% tibble::as_tibble()
testthat::expect_equal(test_df, converted_df)
# Error: `test_df` (`actual`) not equal to `converted_df` (`expected`).

# `attr(actual, '.internal.selfref')` is absent
# `attr(expected, '.internal.selfref')` is a pointer

due to the '.internal.selfref' data.table attribute. I would tend to think this is an undesirable behavior.

@krlmlr
Copy link
Member

krlmlr commented Apr 13, 2024

Thanks. The discussion in #769 is unrelated, the original issue is a genuine bug. Running revdepchecks and releasing to CRAN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants