cols_hide: Hide one or more columns

View source: R/modify_columns.R

cols_hideR Documentation

Hide one or more columns


The cols_hide() function allows us to hide one or more columns from appearing in the final output table. While it's possible and often desirable to omit columns from the input table data before introduction to the gt() function, there can be cases where the data in certain columns is useful (as a column reference during formatting of other columns) but the final display of those columns is not necessary.


cols_hide(data, columns)



The gt table data object

⁠obj:<gt_tbl>⁠ // required

This is the gt table object that is commonly created through use of the gt() function.


Columns to target

⁠<column-targeting expression>⁠ // default: everything()

The columns to hide in the output display table. Can either be a series of column names provided in c(), a vector of column indices, or a select helper function. Examples of select helper functions include starts_with(), ends_with(), contains(), matches(), one_of(), num_range(), and everything().


The hiding of columns is internally a rendering directive, so, all columns that are 'hidden' are still accessible and useful in any expression provided to a rows argument. Furthermore, the cols_hide() function (as with many gt functions) can be placed anywhere in a pipeline of gt function calls (acting as a promise to hide columns when the timing is right). However there's perhaps greater readability when placing this call closer to the end of such a pipeline. The cols_hide() function quietly changes the visible state of a column (much like the cols_unhide() function) and doesn't yield warnings or messages when changing the state of already-invisible columns.


An object of class gt_tbl.


Let's use a small portion of the countrypops dataset to create a gt table. We can hide the country_code_2 and country_code_3 columns with the cols_hide() function.

countrypops |>
  dplyr::filter(country_name == "Egypt") |>
  dplyr::slice_tail(n = 5) |>
  gt() |>
  cols_hide(columns = c(country_code_2, country_code_3))
This image of a table was generated from the first code example in the `cols_hide()` help file.

Using another countrypops-based gt table, we can use the population column to provide the conditional placement of footnotes. Then, we'll hide that column along with the country_code_3 column. Note that the order of the cols_hide() and tab_footnote() statements has no effect on the final display of the table.

countrypops |>
  dplyr::filter(country_name == "Pakistan") |>
  dplyr::slice_tail(n = 5) |>
  gt() |>
  cols_hide(columns = c(country_code_3, population)) |>
    footnote = "Population above 220,000,000.",
    locations = cells_body(
      columns = year,
      rows = population > 220E6
This image of a table was generated from the second code example in the `cols_hide()` help file.

Function ID


Function Introduced

v0.2.0.5 (March 31, 2020)

See Also

cols_unhide() to perform the inverse operation.

Other column modification functions: cols_add(), cols_align_decimal(), cols_align(), cols_label_with(), cols_label(), cols_merge_n_pct(), cols_merge_range(), cols_merge_uncert(), cols_merge(), cols_move_to_end(), cols_move_to_start(), cols_move(), cols_nanoplot(), cols_unhide(), cols_units(), cols_width()

gt documentation built on Oct. 7, 2023, 9:07 a.m.