gt icon indicating copy to clipboard operation
gt copied to clipboard

`rows_hide()` function to compliment `cols_hide()`

Open dereksonderegger opened this issue 2 years ago • 1 comments

Proposal for rows_hide() function

After the gt object has been created and formatted, it is possible to hide particular columns. There are cases were hiding rows would also be useful.

This is especially important for groups of data scientists where gt is the basis of table formatting. For example, complex report tables with titles, footnotes, and other styling choices might get made by some centralized process, but then individuals might want to grab the stylized table, select just certain rows and columns, and then update the title but not have to worry about copying over the footnotes or other formatting details.

# Some function has produced a table
my_table <- iris |>
  group_by(Species) |>
  summarize(across(where(is.numeric), mean)) |>
  gt(rowname_col = 'Species') |>
  # Now have a bunch of formating
  gt::tab_header('Some Title')

# And now I need to take extract just a subset of the rows/colums
# for a report, but we shouldn't copy/paste all of the formatting
# code

my_table |>
  cols_hide(contains('Sepal')) # this works to hide extraneous columns...

# But why isn't there a rows_hide() equivalent? What if I need to 
# hide the setosa row?
my_table |>
  cols_hide(contains('Sepal')) |>
  rows_hide(Species == 'setosa')   # This function doesn't exist.

I would expect that rows_hide() would be able to specify a body row in all the usual ways that cells_body(rows=) can select rows.

Looking into the code for cols_hide(), it appears that there is a flag being set that marks if the column is to be displayed. Is it reasonable to create a corresponding rows flag that will control if a particular row is displayed?

A design question is "What to do if there are summary rows?" Because we are hiding (and not removing), I think that the summary rows shouldn't be changed. However, there should be a way to also hide the summary_row() output.

dereksonderegger avatar Jun 27 '22 19:06 dereksonderegger

This makes a lot of sense and I think it would be a great feature! Agree that summaries shouldn’t be affected but there should be some facility for hiding those rows as well.

rich-iannone avatar Jun 29 '22 00:06 rich-iannone

My endorsements to the proposal.

Dear @dereksonderegger, may I ask if you found a workaround?

I know I can do the following (following your example):

my_table |> as.data.frame() |> filter(Species != 'setosa')

But that leaves us with a data frame, which I don't know how to plug into the well-formatted table... =(

jfgomezok avatar Apr 14 '23 11:04 jfgomezok