Format input time values that are character-based and expressed according to the ISO 8601 time format (HH:MM:SS). Once the appropriate data cells are targeted with columns (and, optionally, rows), we can simply apply a preset time style (see table in info_time_style() for info) to format the times.

fmt_time(data, columns, rows = NULL, time_style = 2)

Arguments

data

a table object that is created using the gt() function.

columns

the columns to format. Can either be a series of column names provided in vars(), a vector of column indices, or a helper function focused on selections. The select helper functions are: starts_with(), ends_with(), contains(), matches(), one_of(), and everything().

rows

optional rows to format. Not providing any value results in all rows in columns being formatted. Can either be a vector of row captions provided c(), a vector of row indices, or a helper function focused on selections. The select helper functions are: starts_with(), ends_with(), contains(), matches(), one_of(), and everything(). We can also use expressions to filter down to the rows we need (e.g., [colname_1] > 100 & [colname_2] < 50).

time_style

the time style to use. Supply a number (from 1 to 5) that corresponds to the preferred time style. Use info_time_style() to see the different numbered and named time presets.

Details

The following time styles are available for simpler formatting of ISO times (all using the input time of 14:35:00 in the example output times):

  1. hms: 14:35:00

  2. hm: 14:35

  3. hms_p: 2:35:00 PM

  4. hm_p: 2:35 PM

  5. h_p: 2 PM

We can use the info_time_style() function for a useful reference on all of the possible inputs to time_style.

Targeting of values is done through columns and additionally by rows (if nothing is provided for rows then entire columns are selected). A number of helper functions exist to make targeting more effective. Conditional formatting is possible by providing a conditional expression to the rows argument. See the Arguments section for more information on this.

Figures

See also

Examples

# Use `exibble` to create a gt table; # keep only the `date` and `time` columns; # format the `time` column to have # times formatted as `hms_p` # (time style `3`) tab_1 <- exibble %>% dplyr::select(date, time) %>% gt() %>% fmt_time( columns = vars(time), time_style = 3 ) # Use `exibble` to create a gt table; # keep only the `date` and `time` columns; # format the `time` column to have mixed # time formats (times after 16:00 will # be different than the others) tab_2 <- exibble %>% dplyr::select(date, time) %>% gt() %>% fmt_time( columns = vars(time), rows = time > "16:00", time_style = 3 ) %>% fmt_time( columns = vars(time), rows = time <= "16:00", time_style = 4 )