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

Use row.names attribute for rowid column #148

Closed
nbenn opened this issue Jan 26, 2019 · 4 comments
Closed

Use row.names attribute for rowid column #148

nbenn opened this issue Jan 26, 2019 · 4 comments
Milestone

Comments

@nbenn
Copy link
Contributor

nbenn commented Jan 26, 2019

I do realize that row names have grown out of fashion in the tidyverse, and that tibble is the main consumer of pillar. However, I feel it would still be worthwhile, if pillar could honor the row.names attribute for printing data.frameish objects. I have tried to implement this using the new_pillar_shaft() API, but I could not come up with anything pleasing (especially if width > terminal width and the output is split up). I'm happy to start a PR.

@krlmlr
Copy link
Member

krlmlr commented Jan 26, 2019

Can you create a colonnade() using the row names as an element?

pillar::colonnade(rlang::list2(rownames = rownames(mtcars), !!!mtcars))
#>    rownames   mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear
#>    <chr>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 Mazda R…  21       6 160     110  3.9   2.62  16.5     0     1     4
#>  2 Mazda R…  21       6 160     110  3.9   2.88  17.0     0     1     4
#>  3 Datsun …  22.8     4 108      93  3.85  2.32  18.6     1     1     4
#>  4 Hornet …  21.4     6 258     110  3.08  3.22  19.4     1     0     3
#>  5 Hornet …  18.7     8 360     175  3.15  3.44  17.0     0     0     3
#>  6 Valiant   18.1     6 225     105  2.76  3.46  20.2     1     0     3
#>  7 Duster …  14.3     8 360     245  3.21  3.57  15.8     0     0     3
#>  8 Merc 24…  24.4     4 147.     62  3.69  3.19  20       1     0     4
#>  9 Merc 230  22.8     4 141.     95  3.92  3.15  22.9     1     0     4
#> 10 Merc 280  19.2     6 168.    123  3.92  3.44  18.3     1     0     4
#> 11 Merc 28…  17.8     6 168.    123  3.92  3.44  18.9     1     0     4
#> 12 Merc 45…  16.4     8 276.    180  3.07  4.07  17.4     0     0     3
#> 13 Merc 45…  17.3     8 276.    180  3.07  3.73  17.6     0     0     3
#> 14 Merc 45…  15.2     8 276.    180  3.07  3.78  18       0     0     3
#> 15 Cadilla…  10.4     8 472     205  2.93  5.25  18.0     0     0     3
#> 16 Lincoln…  10.4     8 460     215  3     5.42  17.8     0     0     3
#> 17 Chrysle…  14.7     8 440     230  3.23  5.34  17.4     0     0     3
#> 18 Fiat 128  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4
#> 19 Honda C…  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4
#> 20 Toyota …  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4
#> 21 Toyota …  21.5     4 120.     97  3.7   2.46  20.0     1     0     3
#> 22 Dodge C…  15.5     8 318     150  2.76  3.52  16.9     0     0     3
#> 23 AMC Jav…  15.2     8 304     150  3.15  3.44  17.3     0     0     3
#> 24 Camaro …  13.3     8 350     245  3.73  3.84  15.4     0     0     3
#> 25 Pontiac…  19.2     8 400     175  3.08  3.84  17.0     0     0     3
#> 26 Fiat X1…  27.3     4  79      66  4.08  1.94  18.9     1     1     4
#> 27 Porsche…  26       4 120.     91  4.43  2.14  16.7     0     1     5
#> 28 Lotus E…  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5
#> 29 Ford Pa…  15.8     8 351     264  4.22  3.17  14.5     0     1     5
#> 30 Ferrari…  19.7     6 145     175  3.62  2.77  15.5     0     1     5
#> 31 Maserat…  15       8 301     335  3.54  3.57  14.6     0     1     5
#> 32 Volvo 1…  21.4     4 121     109  4.11  2.78  18.6     1     1     4

Created on 2019-01-26 by the reprex package (v0.2.1)

@nbenn
Copy link
Contributor Author

nbenn commented Jan 27, 2019

I was able to get even closer to what I wanted by doing

pillar_shaft.row_id_col <- function(x, ...) {
  x <- pillar::style_subtle(format(x))
  pillar::new_pillar_shaft_simple(x, ...)
}

type_sum.row_id_col <- function(x) {
  structure("", class = "type_sum_row_id_col")
}

format_type_sum.type_sum_row_id_col <- function(x, ...) {
  ""
}

my_df <- function(x) {
  x <- cbind(rownames(x), x, stringsAsFactors = FALSE)
  class(x[[1L]]) <- "row_id_col"
  x
}

my_print <- function(x, width = NULL) {
  x <- cbind(rownames(x), x, stringsAsFactors = FALSE)
  class(x[[1L]]) <- "row_id_col"
  out <- pillar::colonnade(x, width = width, has_row_id = FALSE)
  names(out)[1L] <- ""
  pillar::squeeze(out)
}

my_print(head(mtcars))

#>                     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear
#>                   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> Mazda RX4          21       6   160   110  3.9   2.62  16.5     0     1     4
#> Mazda RX4 Wag      21       6   160   110  3.9   2.88  17.0     0     1     4
#> Datsun 710         22.8     4   108    93  3.85  2.32  18.6     1     1     4
#> Hornet 4 Drive     21.4     6   258   110  3.08  3.22  19.4     1     0     3
#> Hornet Sportabout  18.7     8   360   175  3.15  3.44  17.0     0     0     3
#> Valiant            18.1     6   225   105  2.76  3.46  20.2     1     0     3

but unfortunately this fails in a multi-section setting such as

options(width = 60L)
my_print(head(mtcars), width = 80L)

#>                     mpg   cyl  disp    hp  drat    wt  qsec
#>                   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> Mazda RX4          21       6   160   110  3.9   2.62  16.5
#> Mazda RX4 Wag      21       6   160   110  3.9   2.88  17.0
#> Datsun 710         22.8     4   108    93  3.85  2.32  18.6
#> Hornet 4 Drive     21.4     6   258   110  3.08  3.22  19.4
#> Hornet Sportabout  18.7     8   360   175  3.15  3.44  17.0
#> Valiant            18.1     6   225   105  2.76  3.46  20.2
#>    vs    am  gear  carb
#> <dbl> <dbl> <dbl> <dbl>
#>     0     1     4     4
#>     0     1     4     4
#>     1     1     4     1
#>     1     0     3     1
#>     0     0     3     2
#>     1     0     3     1

It would be nice if the row id column would be repeated just like the native (numeric) id behavior. The only reasonable way to do this is to slightly extend the capabilities of rif_shaft et al. I realize that this is no concern for tibble but if pillar is also meant for printing more general data.frame like objects I feel that this could be a worthwhile addition.

@krlmlr
Copy link
Member

krlmlr commented Apr 3, 2021

Let's continue the discussion in #260.

@krlmlr krlmlr closed this as completed Apr 3, 2021
@krlmlr krlmlr added this to the 1.6.0 milestone Apr 11, 2021
@github-actions
Copy link
Contributor

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants