pillar
pillar copied to clipboard
(single) record view
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.
Thanks. Does tibble::glimpse()
help with that?
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.)
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.
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)