-
Notifications
You must be signed in to change notification settings - Fork 12
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
lbl_df() incompatible with dev version of tibble #3
Comments
Ok, so I would remove the Some users suggested this "extension" to the print-method, because they were interested in seeing variable labels when printing (labelled) data frames... But I'm happy with handling over this print-feature to your package. :-) |
Ok, closed by 8f04720 |
Thanks for the quick response. This should do for the CRAN release. However, I'd prefer not to integrate labeled columns in tibbles, but keep them available via sjlabelled. So, one quick way to handle this is to override format.lbl_df <- function(.....) {
x[] <- map(x, add_type_sum_override_class)
NextMethod()
}
add_override_type_sum_class <- function(x) {
class(x) <- c("override_type_sum", class(x))
x
}
type_sum.override_type_sum <- function(x) {
attr(x, "label")
} Note that this will keep working with the current tibble implementation, and the width computation is still correct (not sure about the current implementation). Caveat: you get to keep the The next level would be a custom column header. We don't have callbacks for this in pillar yet, but I'm happy to provide them. Same idea, but you're overriding a different method. This gives support for multiline headers and even more flexibility, e.g., we could support indicating a minimum width for the data, or word wrapping. |
Ok, I implemented your suggestion, but I get an error when converting the numeric vectors to factors (using the current GitHub versions of tibble and pillar):
I'll commit my changes tonight and provide some more information, including reprex. |
My current code:
The first example works with library(tidyverse)
library(strengejacke)
data(efc)
efc %>%
select(e15relat, e16sex, e17age) %>%
as_tibble() %>%
set_label(c("Relationship", "Elder's gender", "Elder's age")) %>%
lbl_df()
#> # A tibble: 908 x 3
#> e15relat e16sex e17age
#> <dbl> <dbl> <dbl>
#> 1 2.00 2.00 83.0
#> 2 2.00 2.00 88.0
#> 3 1.00 2.00 82.0
#> 4 1.00 2.00 67.0
#> 5 2.00 2.00 84.0
#> 6 2.00 2.00 85.0
#> 7 1.00 1.00 74.0
#> 8 4.00 2.00 87.0
#> 9 2.00 2.00 79.0
#> 10 2.00 2.00 83.0
#> # ... with 898 more rows
efc %>%
select(e15relat, e16sex, e17age) %>%
set_label(c("Relationship", "Elder's gender", "Elder's age")) %>%
as_label()
#> # A tibble: 908 x 3
#> e15relat e16sex e17age
#> <fctr> <fctr> <dbl>
#> 1 child female 83.0
#> 2 child female 88.0
#> 3 spouse/partner female 82.0
#> 4 spouse/partner female 67.0
#> 5 child female 84.0
#> 6 child female 85.0
#> 7 spouse/partner male 74.0
#> 8 daughter or son -in-law female 87.0
#> 9 child female 79.0
#> 10 child female 83.0
#> # ... with 898 more rows
efc %>%
select(e15relat, e16sex, e17age) %>%
set_label(c("Relationship", "Elder's gender", "Elder's age")) %>%
as_label() %>%
lbl_df()
#> Error in if (is.infinite(width)) {: argument is of length zero
efc %>%
select(e15relat, e16sex, e17age) %>%
set_label(c("Relationship", "Elder's gender", "Elder's age")) %>%
as_label() %>%
str()
#> Classes 'tbl_df', 'tbl' and 'data.frame': 908 obs. of 3 variables:
#> $ e15relat: Factor w/ 8 levels "spouse/partner",..: 2 2 1 1 2 2 1 4 2 2 ...
#> ..- attr(*, "label")= Named chr "Relationship"
#> .. ..- attr(*, "names")= chr "e15relat"
#> $ e16sex : Factor w/ 2 levels "male","female": 2 2 2 2 2 2 1 2 2 2 ...
#> ..- attr(*, "label")= Named chr "Elder's gender"
#> .. ..- attr(*, "names")= chr "e16sex"
#> $ e17age : atomic 83 88 82 67 84 85 74 87 79 83 ...
#> ..- attr(*, "label")= Named chr "Elder's age"
#> .. ..- attr(*, "names")= chr "e17age" |
The code looks correct, the error is most likely from pillar which supports |
The traceback is here: #3 (comment) |
I'd like to see a traceback for |
Yes, that is the related traceback for this error. |
I got confused, can you please retry the most recent version of pillar? |
Doesn't seem to solve the issue. My current pkg: I have commited the latest changes in sjlabelled via browser, so you might install via GitHub. |
I restarted RStudio / R and now get following error:
|
Thanks. Can you please reinstall pillar, restart RStudio and retry? |
Note that the error is most likely caused by |
Works fine now! I haven't adapted anything yet. I just wonder, why are there no values inside library(tidyverse)
library(strengejacke)
data(efc)
efc %>%
select(e15relat, e16sex, e17age) %>%
as_label() %>%
set_label(c("Relationship", "Elder's gender", "Elder's age")) %>%
lbl_df()
#> # A tibble: 908 x 3
#> e15relat e16sex e17age
#> <> <> <dbl>
#> 1 child female 83.0
#> 2 child female 88.0
#> 3 spouse/partner female 82.0
#> 4 spouse/partner female 67.0
#> 5 child female 84.0
#> 6 child female 85.0
#> 7 spouse/partner male 74.0
#> 8 daughter or son -in-law female 87.0
#> 9 child female 79.0
#> 10 child female 83.0
#> # ... with 898 more rows The attributes are there, so my class-type_sum does not return NULL: efc %>%
select(e15relat, e16sex, e17age) %>%
as_label() %>%
set_label(c("Relationship", "Elder's gender", "Elder's age")) %>%
map(~ attr(.x, "label"))
#> $e15relat
#> e15relat
#> "Relationship"
#>
#> $e16sex
#> e16sex
#> "Elder's gender"
#>
#> $e17age
#> e17age
#> "Elder's age" So, will it be possible to print labels when using the |
Get attributes lost somewhere before printing? |
I set a breakpoint into my type_sum.label_type_sum <- function(x) {
lab <- attr(x, "label")
if (is.null(lab))
"no label"
else
lab
}
|
They may be lost during the call to |
Thanks, works now! library(tidyverse)
library(strengejacke)
data(efc)
efc %>%
select(e15relat, e16sex, e17age) %>%
as_label() %>%
set_label(c("Relationship", "Elder's gender", "Elder's age")) %>%
lbl_df()
#> # A tibble: 908 x 3
#> e15relat e16sex e17age
#> <Relationship> <Elder's gender> <Elder's age>
#> 1 child female 83.0
#> 2 child female 88.0
#> 3 spouse/partner female 82.0
#> 4 spouse/partner female 67.0
#> 5 child female 84.0
#> 6 child female 85.0
#> 7 spouse/partner male 74.0
#> 8 daughter or son -in-law female 87.0
#> 9 child female 79.0
#> 10 child female 83.0
#> # ... with 898 more rows |
I'm seeing check failures with sjlabelled: https://github.com/tidyverse/tibble/blob/e17970597fe63132c42229c22a6c06d653637351/revdep/problems.md#sjlabelled.
tibble is now using pillar to format the columns, this changes the internals of the return value for
trunc_mat()
. It seems thatprint.lbl_df()
is relying on these internals.I'd like to suggest that we work on a better interface in pillar to integrate additional header information in r-lib/pillar#49. So far I was lacking a use case, but labeled data frames seem like a good fit.
The text was updated successfully, but these errors were encountered: