The rm_spanners() function

Use a portion of the gtcars dataset to create a gt table. With tab_spanner(), we can group several related columns together under a spanner column. In this example, that is done with several tab_spanner() calls in order to create two levels of spanner column labels.

gt_tbl <-
  gtcars |>
  dplyr::select(
    -mfr, -trim, bdy_style, drivetrain,
    -drivetrain, -trsmn, -ctry_origin
  ) |>
  dplyr::slice(1:8) |>
  gt(rowname_col = "model") |>
  tab_spanner(label = "HP", columns = c(hp, hp_rpm)) |>
  tab_spanner(label = "Torque", columns = c(trq, trq_rpm)) |>
  tab_spanner(label = "MPG", columns = c(mpg_c, mpg_h)) |>
  tab_spanner(
    label = "Performance",
    columns = c(
      hp, hp_rpm, trq, trq_rpm,
      mpg_c, mpg_h
    )
  )

gt_tbl
Performance
year bdy_style
HP
Torque
MPG
msrp
hp hp_rpm trq trq_rpm mpg_c mpg_h
GT 2017 coupe 647 6250 550 5900 11 18 447000
458 Speciale 2015 coupe 597 9000 398 6000 13 17 291744
458 Spider 2015 convertible 562 9000 398 6000 13 17 263553
458 Italia 2014 coupe 562 9000 398 6000 13 17 233509
488 GTB 2016 coupe 661 8000 561 3000 15 22 245400
California 2015 convertible 553 7500 557 4750 16 23 198973
GTC4Lusso 2017 coupe 680 8250 514 5750 12 17 298000
FF 2015 coupe 652 8000 504 6000 11 16 295000

If you decide that you don’t want any of the spanners in the gt_tbl object, they can all be removed with rm_spanners().

rm_spanners(data = gt_tbl)
year bdy_style hp hp_rpm trq trq_rpm mpg_c mpg_h msrp
GT 2017 coupe 647 6250 550 5900 11 18 447000
458 Speciale 2015 coupe 597 9000 398 6000 13 17 291744
458 Spider 2015 convertible 562 9000 398 6000 13 17 263553
458 Italia 2014 coupe 562 9000 398 6000 13 17 233509
488 GTB 2016 coupe 661 8000 561 3000 15 22 245400
California 2015 convertible 553 7500 557 4750 16 23 198973
GTC4Lusso 2017 coupe 680 8250 514 5750 12 17 298000
FF 2015 coupe 652 8000 504 6000 11 16 295000

Individual spanner column labels can be removed by ID value. In all the above uses of tab_spanner(), the label value is the ID value (you can alternately set a different ID value though the id argument). Let’s remove the "HP" and "MPG" spanner column labels with rm_spanners().

rm_spanners(data = gt_tbl, spanners = c("HP", "MPG"))
Performance
year bdy_style hp hp_rpm
Torque
mpg_c mpg_h msrp
trq trq_rpm
GT 2017 coupe 647 6250 550 5900 11 18 447000
458 Speciale 2015 coupe 597 9000 398 6000 13 17 291744
458 Spider 2015 convertible 562 9000 398 6000 13 17 263553
458 Italia 2014 coupe 562 9000 398 6000 13 17 233509
488 GTB 2016 coupe 661 8000 561 3000 15 22 245400
California 2015 convertible 553 7500 557 4750 16 23 198973
GTC4Lusso 2017 coupe 680 8250 514 5750 12 17 298000
FF 2015 coupe 652 8000 504 6000 11 16 295000

We can also remove spanner column labels by level with rm_spanners(). Provide a vector of one or more values greater than or equal to 1 (the first level starts there). In the next example, we’ll remove the first level of spanner column labels. Any levels not being removed will collapse down accordingly.

rm_spanners(data = gt_tbl, levels = 1)
year bdy_style
Performance
msrp
hp hp_rpm trq trq_rpm mpg_c mpg_h
GT 2017 coupe 647 6250 550 5900 11 18 447000
458 Speciale 2015 coupe 597 9000 398 6000 13 17 291744
458 Spider 2015 convertible 562 9000 398 6000 13 17 263553
458 Italia 2014 coupe 562 9000 398 6000 13 17 233509
488 GTB 2016 coupe 661 8000 561 3000 15 22 245400
California 2015 convertible 553 7500 557 4750 16 23 198973
GTC4Lusso 2017 coupe 680 8250 514 5750 12 17 298000
FF 2015 coupe 652 8000 504 6000 11 16 295000