Skip to content

Commit

Permalink
Include option to hide all column labels (#237)
Browse files Browse the repository at this point in the history
* Add the `column_labels.hidden` argument

* Add option to return an empty string

* Modify function call

* Add line item to `gt_options_default()` tbl

* Update help file using roxygen

* Add tests for `column_labels.hidden = TRUE`
  • Loading branch information
rich-iannone authored Apr 8, 2019
1 parent 876b862 commit 49df7fb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions R/gt_options_default.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gt_options_default <- function() {
"column_labels_background_color", TRUE, "columns", NA_character_,
"column_labels_font_size", TRUE, "columns", "16px",
"column_labels_font_weight", TRUE, "columns", "initial",
"column_labels_hidden", FALSE, "columns", "FALSE",
"row_group_background_color", TRUE, "row_group", NA_character_,
"row_group_font_size", TRUE, "row_group", "16px",
"row_group_font_weight", TRUE, "row_group", "initial",
Expand Down
2 changes: 1 addition & 1 deletion R/render_as_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ render_as_html <- function(data) {
columns_component <-
create_columns_component_h(
boxh_df, output_df, stub_available, spanners_present,
styles_resolved, stubhead_label, col_alignment)
styles_resolved, stubhead_label, col_alignment, opts_df)

# Create the body component of the table
body_component <-
Expand Down
9 changes: 9 additions & 0 deletions R/tab_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#' when striping rows.
#' @param row.striping.include_table_body an option for whether to include the
#' table body when striping rows.
#' @param column_labels.hidden an option to hide the column labels.
#' @return an object of class \code{gt_tbl}.
#' @examples
#' # Use `exibble` to create a gt table with
Expand Down Expand Up @@ -169,6 +170,7 @@ tab_options <- function(data,
column_labels.background.color = NULL,
column_labels.font.size = NULL,
column_labels.font.weight = NULL,
column_labels.hidden = NULL,
row_group.background.color = NULL,
row_group.font.size = NULL,
row_group.font.weight = NULL,
Expand Down Expand Up @@ -329,6 +331,13 @@ tab_options <- function(data,
opts_df <- opts_df_set(opts_df, "column_labels_font_weight", column_labels.font.weight)
}

# column_labels.hidden
if (!is.null(column_labels.hidden)) {

opts_df <- opts_df_set(
opts_df, "column_labels_hidden", column_labels.hidden)
}

# row_group.background.color
if (!is.null(row_group.background.color)) {

Expand Down
13 changes: 12 additions & 1 deletion R/utils_render_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,18 @@ create_columns_component_h <- function(boxh_df,
spanners_present,
styles_resolved,
stubhead_label,
col_alignment) {
col_alignment,
opts_df) {

# Should the column labels be hidden?
column_labels_hidden <-
opts_df %>%
opts_df_get(option = "column_labels_hidden") %>%
as.logical()

if (column_labels_hidden) {
return("")
}

# Get the style attrs for the spanner column headings
spanner_style_attrs <-
Expand Down
9 changes: 6 additions & 3 deletions man/tab_options.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/testthat/test-cols_label.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,29 @@ test_that("the function `cols_label()` works correctly", {
gt(tbl) %>%
cols_label(col_a = "col_1"))
})

test_that("all column labels can be entirely hidden from view", {

# Expect that the option `column_labels.hidden = TRUE` will
# remove the expected node with the classes of `gt_col_heading`
# and `gt_right` (i.e., the column labels)
expect_length(
tbl %>%
gt() %>%
tab_options(column_labels.hidden = TRUE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_col_heading gt_right']"),
0)

# Expect that not hiding the column labels yields a length
# four vector when using the same search
expect_length(
tbl %>%
gt() %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_col_heading gt_right']"),
4)
})

0 comments on commit 49df7fb

Please sign in to comment.