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

(single) record view #317

Open
elbersb opened this issue Apr 22, 2021 · 4 comments
Open

(single) record view #317

elbersb opened this issue Apr 22, 2021 · 4 comments
Milestone

Comments

@elbersb
Copy link

elbersb commented Apr 22, 2021

This may have come up before, but I couldn't find anything in the tibble package or here.

It might be desirable to implement a printing mode that "transposes" the tibble. This came up when demonstrating bloom::glance on a presentation slide, where the output looks like this:

glance(mod)

#> # A tibble: 1 x 12 
#> r.squared adj.r.squared sigma statistic p.value df 
#>     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 
#>       1 0.652 0.652 7.62 3192. 0 1 
#> # ... with 6 more variables: logLik <dbl>, AIC <dbl>, 
#> # BIC <dbl>, deviance <dbl>, df.residual <int>, 
#> # nobs <int>

(the white space got messed up a bit when copying from the PDF)

In this case, all columns are numeric, so the matrix transpose doesn't look so bad:

> t(glance(mod))
                       [,1]
r.squared         0.7168580
adj.r.squared     0.7165251
sigma             6.8773735
statistic      2153.2928344
p.value           0.0000000
df                2.0000000
logLik        -5702.0854721
AIC           11412.1709442
BIC           11433.9338790
deviance      80454.3501172
df.residual    1701.0000000
nobs           1704.0000000

However, tibble could of course support this more fully, including a column for the column type and applying the correct formatting. A similar feature exists in PostgreSQL: https://stackoverflow.com/questions/9604723/alternate-output-format-for-psql. I always found this nice to have.

One could even go so far as treating a single row like a "record", and defaulting to printing it this way. But that might be confusing.

@krlmlr
Copy link
Member

krlmlr commented May 21, 2021

Thanks. Does tibble::glimpse() help with that?

@elbersb
Copy link
Author

elbersb commented May 21, 2021

Sorry, should've mentioned glimpse. This is in fact pretty similar to glimpse, but in its current form, not quite the same. I think there are three issues: glimpse prints all columns by default (so if your data frame has a 1,000 columns, glimpse will print 1,000 lines); glimpse doesn't apply the same useful formatting that print does; and the biggest one is that if you have more than one row, the columns are not aligned:

d <- data.frame(x = c(1, 100), y = c(100, 1))
dplyr::glimpse(d)
#> Rows: 2
#> Columns: 2
#> $ x <dbl> 1, 100
#> $ y <dbl> 100, 1

Created on 2021-05-21 by the reprex package (v2.0.0)

(I realize that this is probably a pretty niche issue, so feel free to close if you think this is out of scope.)

@krlmlr
Copy link
Member

krlmlr commented May 24, 2021

Thanks. Should we add an align = FALSE and max = Inf argument to glimpse() ?

I don't understand "the same useful formatting that print() does", can you please rephrase?

Other than that, I think most if not all of the tools to build custom views of the data are exported by pillar, please let me know if anything's missing.

@elbersb
Copy link
Author

elbersb commented May 25, 2021

Yes, align = FALSE and max = Inf would go a long way!

Regarding the second point, glimpse doesn't apply the same formatting that print does. For instance, in the following example the labelled column is displayed differently, and the rounding is not applied for the numeric vector. The negative numbers also show up with red font color in the print call, but not in glimpse (of course, this is not visible in the reprex).

library("tibble")
df <- tibble(
  s = haven::labelled(c("M", "M", "F"), c(Male = "M", Female = "F")),
  x = rnorm(3)
)
df
#> # A tibble: 3 x 2
#>   s                x
#>   <chr+lbl>    <dbl>
#> 1 M [Male]    0.0849
#> 2 M [Male]    0.842 
#> 3 F [Female] -0.481
glimpse(df)
#> Rows: 3
#> Columns: 2
#> $ s <chr+lbl> "M", "M", "F"
#> $ x <dbl> 0.08492588, 0.84195136, -0.48107764

Created on 2021-05-25 by the reprex package (v2.0.0)

@krlmlr krlmlr modified the milestones: 1.6.5, 1.7.0 Dec 25, 2021
@krlmlr krlmlr modified the milestones: 1.7.0, 1.9.0 Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants