Practice is good. We learned a great number of functions in modules 01 and 02. Here they are as a list:
01gt()gt_preview()gtsave()02tab_stubhead()tab_spanner()tab_header()tab_source_note()tab_footnote()tab_style()tab_options()contains()matches()starts_with()ends_with()everything()md()html()cells_title()cells_stubhead()cells_column_spanners()cells_column_labels()cells_row_groups()cells_stub()cells_body()cells_summary()cells_grand_summary()cell_text()cell_fill()cell_borders()That’s a lot. In a short amount of time we can still get useful practice by trying a few things out.
Try modifying this statement several times and see what happens (e.g., leave out the first argument, change values, add a few other args with help from ,<TAB> after "group").
exibble %>% gt(rowname_col = "row", groupname_col = "group")
| num | char | fctr | date | time | datetime | currency | |
|---|---|---|---|---|---|---|---|
| grp_a | |||||||
| row_1 | 1.111e-01 | apricot | one | 2015-01-15 | 13:35 | 2018-01-01 02:22 | 49.950 |
| row_2 | 2.222e+00 | banana | two | 2015-02-15 | 14:40 | 2018-02-02 14:33 | 17.950 |
| row_3 | 3.333e+01 | coconut | three | 2015-03-15 | 15:45 | 2018-03-03 03:44 | 1.390 |
| row_4 | 4.444e+02 | durian | four | 2015-04-15 | 16:50 | 2018-04-04 15:55 | 65100.000 |
| grp_b | |||||||
| row_5 | 5.550e+03 | NA | five | 2015-05-15 | 17:55 | 2018-05-05 04:00 | 1325.810 |
| row_6 | NA | fig | six | 2015-06-15 | NA | 2018-06-06 16:11 | 13.255 |
| row_7 | 7.770e+05 | grapefruit | seven | NA | 19:10 | 2018-07-07 05:22 | NA |
| row_8 | 8.880e+06 | honeydew | eight | 2015-08-15 | 20:20 | NA | 0.440 |
How about some table header fun? Try adapting this snippet of code to make a great set of title and subtitle. Use md() to get Markdown in there. Know HTML? Try your hand at some HTML titling with the html() helper. Finish it off with a gtsave() to your preferred output type.
gtcars %>%
dplyr::select(mfr, model, msrp) %>%
dplyr::slice(1:5) %>%
gt() %>%
tab_header(
title = "<title>",
subtitle = "<subtitle>"
)
| <title> | ||
|---|---|---|
| <subtitle> | ||
| mfr | model | msrp |
| Ford | GT | 447000 |
| Ferrari | 458 Speciale | 291744 |
| Ferrari | 458 Spider | 263553 |
| Ferrari | 458 Italia | 233509 |
| Ferrari | 488 GTB | 245400 |
Make your gt table a bit more stylish. We need to use the combination of tab_style(), one or more location helper functions (e.g,. cells_body(), cells_column_labels(), etc.), and a styling function like cell_fill(). Here’s an example table with a number of different location types. Try changing the background of different locations.
exibble %>%
gt(rowname_col = "row", groupname_col = "group") %>%
tab_header(title = "The title", subtitle = "The subtitle") %>%
tab_source_note("A source note.") %>%
tab_footnote(
footnote = "A footnote",
locations = cells_body(columns = 1, rows = 1)
) %>%
tab_style(
style = cell_fill(color = "#E5873A"),
locations = cells_body(columns = char, rows = 1)
)
| The title | |||||||
|---|---|---|---|---|---|---|---|
| The subtitle | |||||||
| num | char | fctr | date | time | datetime | currency | |
| grp_a | |||||||
| row_1 | 1.111e-011 | apricot | one | 2015-01-15 | 13:35 | 2018-01-01 02:22 | 49.950 |
| row_2 | 2.222e+00 | banana | two | 2015-02-15 | 14:40 | 2018-02-02 14:33 | 17.950 |
| row_3 | 3.333e+01 | coconut | three | 2015-03-15 | 15:45 | 2018-03-03 03:44 | 1.390 |
| row_4 | 4.444e+02 | durian | four | 2015-04-15 | 16:50 | 2018-04-04 15:55 | 65100.000 |
| grp_b | |||||||
| row_5 | 5.550e+03 | NA | five | 2015-05-15 | 17:55 | 2018-05-05 04:00 | 1325.810 |
| row_6 | NA | fig | six | 2015-06-15 | NA | 2018-06-06 16:11 | 13.255 |
| row_7 | 7.770e+05 | grapefruit | seven | NA | 19:10 | 2018-07-07 05:22 | NA |
| row_8 | 8.880e+06 | honeydew | eight | 2015-08-15 | 20:20 | NA | 0.440 |
| A source note. | |||||||
|
1
A footnote
|
|||||||