Skip to contents

We can easily move set of columns to the beginning of the column series and we only need to specify which columns. It's possible to do this upstream of gt, however, it is easier with this function and it presents less possibility for error. The ordering of the columns that are moved to the start is preserved (same with the ordering of all other columns in the table).

Usage

cols_move_to_start(data, columns)

Arguments

data

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

Columns to target

<column-targeting expression> // required

The columns for which the moving operations should be applied. Can either be a series of column names provided in c(), a vector of column indices, or a select helper function (e.g. starts_with(), ends_with(), contains(), matches(), num_range(), and everything()). The columns move as a group to the left-most side of the table. The order of the remaining columns will be preserved.

Value

An object of class gt_tbl.

Details

The columns supplied in columns must all exist in the table. If you need to place one or columns at the end of the column series, cols_move_to_end() should be used. More control is offered with cols_move(), where columns could be placed after a specific column.

Examples

For this example, we'll use a portion of the countrypops dataset to create a simple gt table. Let's move the year column, which is the middle column, to the start of the column series with cols_move_to_start().

countrypops |>
  dplyr::select(-contains("code")) |>
  dplyr::filter(
    country_name == "Fiji",
    year %in% 2017:2021
  ) |>
  gt() |>
  cols_move_to_start(columns = year)

This image of a table was generated from the first code example in the `cols_move_to_start()` help file.

We can also move multiple columns at a time. With the same countrypops-based table, let's move both the year and population columns to the start of the column series.

countrypops |>
  dplyr::select(-contains("code")) |>
  dplyr::filter(
    country_name == "Fiji",
    year %in% 2017:2021
  ) |>
  gt() |>
  cols_move_to_start(columns = c(year, population))

This image of a table was generated from the second code example in the `cols_move_to_start()` help file.

Function ID

5-10

Function Introduced

v0.2.0.5 (March 31, 2020)