Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable table striping options #236

Merged
merged 10 commits into from
May 1, 2019
4 changes: 2 additions & 2 deletions R/gt_options_default.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ gt_options_default <- function() {
"table_body_border_bottom_width", TRUE, "table_body", "2px",
"table_body_border_bottom_color", TRUE, "table_body", "#A8A8A8",
"row_padding", TRUE, "row", "8px",
"row_striping_include_stub", TRUE, "row", "TRUE",
"row_striping_include_table_body", TRUE, "row", "TRUE",
"row_striping_include_stub", FALSE, "row", "FALSE",
"row_striping_include_table_body", FALSE, "row", "TRUE",
"summary_row_background_color", TRUE, "summary_row", NA_character_,
"summary_row_padding", TRUE, "summary_row", "8px",
"summary_row_text_transform", TRUE, "summary_row", "inherit",
Expand Down
2 changes: 1 addition & 1 deletion R/render_as_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ render_as_html <- function(data) {
create_body_component_h(
row_splits_body, row_splits_styles, styles_resolved,
groups_rows_df, col_alignment, stub_components, summaries_present,
list_of_summaries, n_rows, n_cols)
list_of_summaries, n_rows, n_cols, opts_df)

# Create the source note rows and handle any available footnotes
source_note_component <-
Expand Down
25 changes: 20 additions & 5 deletions R/utils_render_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ create_body_component_h <- function(row_splits_body,
summaries_present,
list_of_summaries,
n_rows,
n_cols) {

n_cols,
opts_df) {

if (is.null(stub_components)) {
stub_available <- FALSE
Expand All @@ -684,6 +684,18 @@ create_body_component_h <- function(row_splits_body,
dplyr::mutate(group_label = gsub("^NA", "\u2014", group_label))
}

# Is the stub to be striped?
stub_striped <-
opts_df %>%
opts_df_get(option = "row_striping_include_stub") %>%
as.logical()

# Are the rows in the table body to be striped?
table_body_striped <-
opts_df %>%
opts_df_get(option = "row_striping_include_table_body") %>%
as.logical()

body_rows <- c()

for (i in 1:n_rows) {
Expand Down Expand Up @@ -732,12 +744,14 @@ create_body_component_h <- function(row_splits_body,
paste0(
"<tr>\n",
paste0(
"<td class='gt_row gt_stub gt_", col_alignment[1], "'",
"<td class='gt_row gt_stub gt_", col_alignment[1],
ifelse(i %% 2 == 0 && stub_striped, " gt_striped", ""), "'",
create_style_attrs(row_splits_styles[[i]][1]),
">", row_splits_body[[i]][1],
"</td>"), "\n",
paste0(
"<td class='gt_row gt_", col_alignment[-1], ifelse(i %% 2 == 0, " gt_striped", ""), "'",
"<td class='gt_row gt_", col_alignment[-1],
ifelse(i %% 2 == 0 && table_body_striped, " gt_striped", ""), "'",
create_style_attrs(row_splits_styles[[i]][-1]),
">", row_splits_body[[i]][-1],
"</td>", collapse = "\n"),
Expand All @@ -753,7 +767,8 @@ create_body_component_h <- function(row_splits_body,
paste0(
"<tr>\n",
paste0(
"<td class='gt_row gt_", col_alignment, ifelse(i %% 2 == 0, " gt_striped", ""), "'",
"<td class='gt_row gt_", col_alignment,
ifelse(i %% 2 == 0 && table_body_striped, " gt_striped", ""), "'",
create_style_attrs(row_splits_styles[[i]]),
">", row_splits_body[[i]],
"</td>", collapse = "\n"),
Expand Down
26 changes: 0 additions & 26 deletions tests/testthat/test-cols_label.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,3 @@ test_that("the function `cols_label()` works correctly", {
gt(tbl) %>%
cols_label(col_a = "col_1"))
})

test_that("all column labels can be entirely hidden from view", {

# Expect that the option `column_labels.hidden = TRUE` will
# remove the expected node with the classes of `gt_col_heading`
# and `gt_right` (i.e., the column labels)
expect_length(
tbl %>%
gt() %>%
tab_options(column_labels.hidden = TRUE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_col_heading gt_right']"),
0)

# Expect that not hiding the column labels yields a length
# four vector when using the same search
expect_length(
tbl %>%
gt() %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_col_heading gt_right']"),
4)
})

118 changes: 116 additions & 2 deletions tests/testthat/test-tab_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -690,14 +690,14 @@ test_that("the internal `opts_df` table can be correctly modified", {
expect_equal(c("4px", "3px"))

# Modify the `row.striping.include_stub` option
tbl_html <- data %>% tab_options(row.striping.include_stub = FALSE)
tbl_html <- data %>% tab_options(row.striping.include_stub = TRUE)

# Compare before and after values
c(opts_df_1 %>%
dplyr::filter(parameter == "row_striping_include_stub") %>% dplyr::pull(value),
attr(tbl_html, "opts_df", exact = TRUE) %>%
dplyr::filter(parameter == "row_striping_include_stub") %>% dplyr::pull(value)) %>%
expect_equal(c("TRUE", "FALSE"))
expect_equal(c("FALSE", "TRUE"))

# Modify the `row.striping.include_table_body` option
tbl_html <- data %>% tab_options(row.striping.include_table_body = FALSE)
Expand Down Expand Up @@ -746,3 +746,117 @@ test_that("the `opts_df` getter/setter both function properly", {
opts_df_get(option = "footnote_font_size") %>%
expect_equal("60%")
})

test_that("all column labels can be entirely hidden from view", {

# Expect that the option `column_labels.hidden = TRUE` will
# remove the expected node with the classes of `gt_col_heading`
# and `gt_right` (i.e., the column labels)
expect_length(
tbl %>%
gt() %>%
tab_options(column_labels.hidden = TRUE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_col_heading gt_right']"),
0)

# Expect that not hiding the column labels yields a length
# four vector when using the same search
expect_length(
tbl %>%
gt() %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_col_heading gt_right']"),
4)
})

test_that("the row striping options work correctly", {

# Expect that the option `row.striping.include_stub = FALSE`
# will result in no CSS class combinations of `gt_stub` and
# `gt_striped`
expect_length(
tbl %>%
gt() %>%
tab_options(row.striping.include_stub = FALSE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_row gt_stub gt_left gt_striped']"),
0)

# Expect that the option `row.striping.include_stub = TRUE` will
# result in a particular class combination for every second
# stub cell (includes `gt_striped`)
expect_length(
tbl %>%
gt() %>%
tab_options(row.striping.include_stub = TRUE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_row gt_stub gt_left gt_striped']"),
5)

# Expect that the option `row.striping.include_table_body = TRUE` will
# result in a particular class combination for every second
# stub cell (includes `gt_striped`)
expect_length(
tbl %>%
gt() %>%
tab_options(row.striping.include_table_body = TRUE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_row gt_right gt_striped']"),
20)

# Expect that the options `row.striping.include_table_body = TRUE`
# and `row.striping.include_stub = TRUE` will result in cells that
# have either of two class combinations for every second stub cell
# (both include `gt_striped`)
expect_length(
c(
tbl %>%
gt() %>%
tab_options(
row.striping.include_stub = TRUE,
row.striping.include_table_body = TRUE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_row gt_stub gt_left gt_striped']"),
tbl %>%
gt() %>%
tab_options(
row.striping.include_stub = TRUE,
row.striping.include_table_body = TRUE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_row gt_right gt_striped']")
),
25)

# Expect that the options `row.striping.include_table_body = FALSE`
# and `row.striping.include_stub = FALSE` will result in cells that
# have neither of two class combinations in every second stub cell
# (with `gt_striped`)
expect_length(
c(
tbl %>%
gt() %>%
tab_options(
row.striping.include_stub = FALSE,
row.striping.include_table_body = FALSE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_row gt_stub gt_left gt_striped']"),
tbl %>%
gt() %>%
tab_options(
row.striping.include_stub = FALSE,
row.striping.include_table_body = FALSE) %>%
render_as_html() %>%
xml2::read_html() %>%
selection_text("[class='gt_row gt_right gt_striped']")
),
0)
})