Skip to content

Commit cff38e6

Browse files
authored
Merge pull request #2127 from rstudio/feat-rows-hide
feat: add the `rows_hide()` and `rows_unhide()` functions
2 parents 94d774c + 6e2f562 commit cff38e6

19 files changed

+1006
-11
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ export(row_group)
206206
export(row_group_order)
207207
export(row_order)
208208
export(rows_add)
209+
export(rows_hide)
210+
export(rows_unhide)
209211
export(starts_with)
210212
export(stub)
211213
export(sub_large_vals)

R/dt_groups_rows.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ dt_groups_rows_build <- function(data, context) {
5656
table_body <- dt_data_get(data = data)
5757
# For multiple stub columns, use the rightmost (primary) column for rowname
5858
primary_stub_var <- stub_var[length(stub_var)]
59-
stub_df[["rowname"]] <- as.character(table_body[[primary_stub_var]])
59+
# Use rownum_i to get the correct row values (handles hidden rows)
60+
stub_df[["rowname"]] <- as.character(table_body[[primary_stub_var]][stub_df$rownum_i])
6061
}
6162
# what happens if dt_stub_df doesn't exist?
6263

@@ -87,6 +88,10 @@ dt_groups_rows_build <- function(data, context) {
8788
groups_rows[i, "row_end"] <- max(rows_matched)
8889
}
8990

91+
# Remove groups that have no visible rows (row_start/row_end are NA)
92+
# This can happen when all rows in a group are hidden via rows_hide()
93+
groups_rows <- groups_rows[!is.na(groups_rows$row_start), ]
94+
9095
# Join `built_group_label` values to the `groups_rows` table
9196
if (nrow(groups_rows) > 0) {
9297

R/dt_rows_hidden.R

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#------------------------------------------------------------------------------#
2+
#
3+
# /$$
4+
# | $$
5+
# /$$$$$$ /$$$$$$
6+
# /$$__ $$|_ $$_/
7+
# | $$ \ $$ | $$
8+
# | $$ | $$ | $$ /$$
9+
# | $$$$$$$ | $$$$/
10+
# \____ $$ \___/
11+
# /$$ \ $$
12+
# | $$$$$$/
13+
# \______/
14+
#
15+
# This file is part of the 'rstudio/gt' project.
16+
#
17+
# Copyright (c) 2018-2026 gt authors
18+
#
19+
# For full copyright and license information, please look at
20+
# https://gt.rstudio.com/LICENSE.html
21+
#
22+
#------------------------------------------------------------------------------#
23+
24+
25+
.dt_rows_hidden_key <- "_rows_hidden"
26+
27+
dt_rows_hidden_get <- function(data) {
28+
dt__get(data, .dt_rows_hidden_key)
29+
}
30+
31+
dt_rows_hidden_set <- function(data, rows_hidden) {
32+
dt__set(data, .dt_rows_hidden_key, rows_hidden)
33+
}
34+
35+
dt_rows_hidden_init <- function(data) {
36+
dt_rows_hidden_set(data = data, rows_hidden = integer(0L))
37+
}
38+
39+
#' Add rows to the hidden rows list
40+
#'
41+
#' @param data The gt table data object.
42+
#' @param rows Integer vector of row indices to hide.
43+
#'
44+
#' @noRd
45+
dt_rows_hidden_add <- function(data, rows) {
46+
47+
existing_hidden <- dt_rows_hidden_get(data = data)
48+
49+
# Combine and keep unique values
50+
51+
rows_hidden <- sort(unique(c(existing_hidden, rows)))
52+
53+
dt_rows_hidden_set(data = data, rows_hidden = rows_hidden)
54+
}
55+
56+
#' Remove rows from the hidden rows list (unhide them)
57+
#'
58+
#' @param data The gt table data object.
59+
#' @param rows Integer vector of row indices to unhide.
60+
#'
61+
#' @noRd
62+
dt_rows_hidden_remove <- function(data, rows) {
63+
64+
existing_hidden <- dt_rows_hidden_get(data = data)
65+
66+
# Remove specified rows from hidden list
67+
rows_hidden <- setdiff(existing_hidden, rows)
68+
69+
dt_rows_hidden_set(data = data, rows_hidden = rows_hidden)
70+
}
71+
72+
#' Check if any rows are hidden
73+
#'
74+
#' @param data The gt table data object.
75+
#'
76+
#' @noRd
77+
dt_rows_hidden_exist <- function(data) {
78+
length(dt_rows_hidden_get(data = data)) > 0
79+
}

R/dt_stub_df.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,33 @@ reorder_stub_df <- function(data) {
246246
# Then, apply any row ordering directives
247247
stub_df <- apply_row_order_directives(data = data, stub_df = stub_df)
248248

249+
# Finally, filter out any hidden rows
250+
stub_df <- filter_hidden_rows(data = data, stub_df = stub_df)
251+
249252
dt_stub_df_set(data = data, stub_df = stub_df)
250253
}
251254

255+
# Function to filter out hidden rows from stub_df
256+
filter_hidden_rows <- function(data, stub_df) {
257+
258+
# Get the hidden rows
259+
260+
hidden_rows <- dt_rows_hidden_get(data = data)
261+
262+
# If there are no hidden rows, return stub_df unchanged
263+
if (length(hidden_rows) == 0) {
264+
return(stub_df)
265+
}
266+
267+
# Filter out rows where rownum_i is in the hidden list
268+
stub_df <- stub_df[!stub_df$rownum_i %in% hidden_rows, ]
269+
270+
# Reset row names
271+
rownames(stub_df) <- NULL
272+
273+
stub_df
274+
}
275+
252276
# Function to apply the lazy row ordering directives captured by `row_order()`
253277
apply_row_order_directives <- function(data, stub_df) {
254278

R/gt.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ gt <- function(
389389
)
390390
data <- dt_row_groups_init(data = data)
391391
data <- dt_row_order_init(data = data)
392+
data <- dt_rows_hidden_init(data = data)
392393
data <- dt_heading_init(data = data)
393394
data <- dt_spanners_init(data = data)
394395
data <- dt_stubhead_init(data = data)

0 commit comments

Comments
 (0)