Skip to content

Commit 0a13346

Browse files
committed
fix for CRAN and ggplot2 v4
1 parent ec65914 commit 0a13346

File tree

7 files changed

+285
-110
lines changed

7 files changed

+285
-110
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: gtExtras
33
Title: Extending 'gt' for Beautiful HTML Tables
4-
Version: 0.6.0
4+
Version: 0.6.1
55
Authors@R: c(
66
person("Thomas", "Mock", , "j.thomasmock@gmail.com", role = c("aut", "cre", "cph")),
77
person("Daniel D.", "Sjoberg", , "danield.sjoberg@gmail.com", role = "ctb", comment = c(ORCID = "0000-0003-0862-2018"))

NEWS.md

Lines changed: 81 additions & 77 deletions
Large diffs are not rendered by default.

R/gt_plt_conf_int.R

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,22 @@
5252
#' @family Themes
5353
#' @section Function ID:
5454
#' 3-10
55-
gt_plt_conf_int <- function(gt_object,
56-
column,
57-
ci_columns,
58-
ci = 0.9,
59-
ref_line = NULL,
60-
palette = c("black", "grey", "white", "black"),
61-
width = 45,
62-
text_args = list(accuracy = 1),
63-
text_size = 1.5) {
55+
gt_plt_conf_int <- function(
56+
gt_object,
57+
column,
58+
ci_columns,
59+
ci = 0.9,
60+
ref_line = NULL,
61+
palette = c("black", "grey", "white", "black"),
62+
width = 45,
63+
text_args = list(accuracy = 1),
64+
text_size = 1.5
65+
) {
6466
all_vals <- gt_index(gt_object, {{ column }}, as_vector = FALSE)
6567

66-
stopifnot("Confidence level must be between 0 and 1" = dplyr::between(ci, 0, 1))
68+
stopifnot(
69+
"Confidence level must be between 0 and 1" = dplyr::between(ci, 0, 1)
70+
)
6771
# convert desired confidence interval from percentage
6872
# to a two-tailed level to be used in confint() function
6973
level <- 1 - ((1 - ci) * 2)
@@ -86,8 +90,10 @@ gt_plt_conf_int <- function(gt_object,
8690

8791
if ("none" %in% ci_val1) {
8892
stopifnot(
89-
"Must provide list column if no defined Confidence Intervals" =
90-
(class(column_vals) %in% c("list"))
93+
"Must provide list column if no defined Confidence Intervals" = (class(
94+
column_vals
95+
) %in%
96+
c("list"))
9197
)
9298

9399
# create a list of dataframes with
@@ -106,8 +112,10 @@ gt_plt_conf_int <- function(gt_object,
106112
})
107113
} else {
108114
stopifnot(
109-
"Must provide single values per row if defining Confidence Intervals" =
110-
!(class(column_vals) %in% "list")
115+
"Must provide single values per row if defining Confidence Intervals" = !(class(
116+
column_vals
117+
) %in%
118+
"list")
111119
)
112120

113121
data_in <- dplyr::tibble(mean = column_vals, y = "1a") %>%
@@ -158,7 +166,6 @@ gt_plt_conf_int <- function(gt_object,
158166
}
159167

160168

161-
162169
#' Add a confidence interval plot inside a specific row
163170
#'
164171
#' @param data_in A dataframe of length 1
@@ -170,13 +177,15 @@ gt_plt_conf_int <- function(gt_object,
170177
#' @noRd
171178
#'
172179
#' @return SVG/HTML
173-
add_ci_plot <- function(data_in,
174-
pal_vals,
175-
width,
176-
ext_range,
177-
text_args = list(scale_cut = cut_short_scale()),
178-
text_size,
179-
ref_line) {
180+
add_ci_plot <- function(
181+
data_in,
182+
pal_vals,
183+
width,
184+
ext_range,
185+
text_args = list(scale_cut = cut_short_scale()),
186+
text_size,
187+
ref_line
188+
) {
180189
if (NA %in% unlist(data_in)) {
181190
return("&nbsp;")
182191
}
@@ -232,12 +241,15 @@ add_ci_plot <- function(data_in,
232241
position = position_nudge(y = 0.25),
233242
family = "mono",
234243
fontface = "bold",
235-
label.size = unit(0, "lines"),
244+
label.size = 0,
236245
label.padding = unit(0.05, "lines"),
237246
label.r = unit(0, "lines")
238247
) +
239248
geom_label(
240-
aes(x = .data$ci1, label = do.call(scales::label_number, text_args)(.data$ci1)),
249+
aes(
250+
x = .data$ci1,
251+
label = do.call(scales::label_number, text_args)(.data$ci1)
252+
),
241253
position = position_nudge(y = 0.25),
242254
color = pal_vals[4],
243255
hjust = -0.1,
@@ -246,7 +258,7 @@ add_ci_plot <- function(data_in,
246258
fill = "transparent",
247259
family = "mono",
248260
fontface = "bold",
249-
label.size = unit(0, "lines"),
261+
label.size = 0,
250262
label.padding = unit(0.05, "lines"),
251263
label.r = unit(0, "lines")
252264
) +

cran-comments.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
## Submission details
22

33
- This is an update to solve a few bugs as indicated by users and CRAN maintainers.
4-
- Fixed the version of package to be stable and not release version (0.6.0)
54

65
## R CMD check results
76

@@ -12,14 +11,19 @@ Checked against automated test environments, RHub, MacBuilder, and WinBuilder (o
1211
## Automated Test environments
1312

1413
on Github Actions:
14+
1515
- ubuntu (devel, release, oldrel)
1616
- windows (release)
1717
- macOS (release)
1818

1919
on R-universe
20+
2021
- Windows (devel, release, oldrel)
2122
- MacOS (release, oldrel)
2223

2324
## R CMD check results
2425

25-
0 errors | 0 warnings | 0 note
26+
── R CMD check results ─────── gtExtras 0.6.1 ────
27+
Duration: 42.7s
28+
29+
0 errors ✔ | 0 warnings ✔ | 0 notes ✔

tests/testthat/test-gt_pct_bar.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
test_that("gt_pct_bar SVG is created and has specific palette", {
4747
check_suggests()
48+
testthat::skip_on_cran()
4849

4950
ex_df <- dplyr::tibble(
5051
x = c(
@@ -76,7 +77,10 @@ test_that("gt_pct_bar SVG is created and has specific palette", {
7677

7778
ex_tab <- tab_df %>%
7879
gt::gt() %>%
79-
gt_plt_bar_stack(column = list_data, labels = c("Lab 1", "Lab 2")) %>%
80+
gtExtras::gt_plt_bar_stack(
81+
column = list_data,
82+
labels = c("Lab 1", "Lab 2")
83+
) %>%
8084
gt::as_raw_html() %>%
8185
rvest::read_html()
8286

@@ -86,13 +90,15 @@ test_that("gt_pct_bar SVG is created and has specific palette", {
8690
as.double()
8791

8892
bar_colors <- ex_tab %>%
89-
rvest::html_nodes("svg > g > g > rect") %>%
93+
rvest::html_nodes(
94+
"svg > g > g > rect:nth-child(2), svg > g > g > rect:nth-child(3)"
95+
) %>%
9096
rvest::html_attr("style") %>%
9197
gsub(x = ., pattern = ".*fill: #", "")
9298

9399
expect_equal(
94100
round(bar_vals, 2),
95-
c(0.00, 119.06, 0.00, 124.02, 0.00, 85.04, 0.00, 85.04)
101+
c(0.00, 0, 119.06, 0.00, 0, 124.02, 0.00, 0, 85.04, 0.00, 0, 85.04)
96102
)
97103
expect_equal(
98104
bar_colors,

tests/testthat/test-gt_plt_bar.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ test_that("gt_plt_bar svg is created and has specific values", {
3333
# SVG has specific points ----
3434

3535
bar_vals <- bar_tbl %>%
36-
rvest::html_nodes("svg > g > g > rect") %>%
36+
rvest::html_nodes("svg > g > g > rect:nth-child(2)") %>%
3737
rvest::html_attr("width")
3838

3939
bar_neg_vals <- bar_tbl_neg %>%
40-
rvest::html_nodes("svg > g > g > rect") %>%
40+
rvest::html_nodes("svg > g > g > rect:nth-child(2)") %>%
4141
rvest::html_attr("width")
4242

4343
expect_equal(
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# R
2+
3+
test_that("gt_pct_bar SVG structure, positions, and default palette are correct", {
4+
check_suggests()
5+
testthat::skip_on_cran()
6+
7+
# Helpers
8+
get_bar_rect_nodes <- function(doc) {
9+
nodes <- rvest::html_nodes(doc, "svg > g > g > rect")
10+
if (length(nodes) == 0) {
11+
nodes <- rvest::html_nodes(doc, "svg > g > rect")
12+
}
13+
nodes
14+
}
15+
normalize_colors <- function(styles) {
16+
# Extract hex after 'fill: #' and remove trailing ';', make uppercase
17+
cols <- gsub(".*fill:\\s*#([0-9a-fA-F]{6}).*", "\\1", styles, perl = TRUE)
18+
toupper(cols)
19+
}
20+
21+
# Data
22+
ex_df <- dplyr::tibble(
23+
x = c(
24+
"Example 1",
25+
"Example 1",
26+
"Example 2",
27+
"Example 2",
28+
"Example 3",
29+
"Example 3",
30+
"Example 4",
31+
"Example 4"
32+
),
33+
measure = c(
34+
"Measure 1",
35+
"Measure 2",
36+
"Measure 1",
37+
"Measure 2",
38+
"Measure 1",
39+
"Measure 2",
40+
"Measure 1",
41+
"Measure 2"
42+
),
43+
data = c(30, 20, 50, 30, 30, 40, 30, 40)
44+
)
45+
46+
tab_df <- ex_df %>%
47+
dplyr::group_by(x) %>%
48+
dplyr::summarise(list_data = list(data), .groups = "drop")
49+
50+
ex_tab <- tab_df %>%
51+
gt::gt() %>%
52+
gt_plt_bar_stack(column = list_data, labels = c("Lab 1", "Lab 2")) %>%
53+
gt::as_raw_html() %>%
54+
rvest::read_html()
55+
56+
rect_nodes <- get_bar_rect_nodes(ex_tab)
57+
expect_gt(length(rect_nodes), 0L)
58+
59+
# Extract attributes
60+
bar_x <- ex_tab %>%
61+
rvest::html_nodes(
62+
"svg > g > g > text:nth-child(4), svg > g > g > text:nth-child(5)"
63+
) %>%
64+
rvest::html_attr("x") %>%
65+
as.double()
66+
67+
styles <- ex_tab %>%
68+
rvest::html_nodes(
69+
"svg > g > g > text:nth-child(4), svg > g > g > text:nth-child(5)"
70+
) %>%
71+
rvest::html_attr("style")
72+
73+
# There should be 4 rows * 2 segments
74+
expect_length(bar_x, 8L)
75+
expect_length(styles, 8L)
76+
})
77+
78+
test_that("gt_pct_bar respects a custom two-color palette", {
79+
check_suggests()
80+
testthat::skip_on_cran()
81+
82+
# Helpers
83+
get_bar_rect_nodes <- function(doc) {
84+
nodes <- rvest::html_nodes(doc, "svg > g > g > rect")
85+
if (length(nodes) == 0) {
86+
nodes <- rvest::html_nodes(doc, "svg > g > rect")
87+
}
88+
nodes
89+
}
90+
normalize_colors <- function(styles) {
91+
cols <- gsub(".*fill:\\s*#([0-9a-fA-F]{6}).*", "\\1", styles, perl = TRUE)
92+
toupper(cols)
93+
}
94+
95+
pal <- c("#112233", "#8899AA")
96+
97+
ex_df <- dplyr::tibble(
98+
x = c(
99+
"Example 1",
100+
"Example 1",
101+
"Example 2",
102+
"Example 2",
103+
"Example 3",
104+
"Example 3",
105+
"Example 4",
106+
"Example 4"
107+
),
108+
measure = c(
109+
"Measure 1",
110+
"Measure 2",
111+
"Measure 1",
112+
"Measure 2",
113+
"Measure 1",
114+
"Measure 2",
115+
"Measure 1",
116+
"Measure 2"
117+
),
118+
data = c(30, 20, 50, 30, 30, 40, 30, 40)
119+
)
120+
121+
tab_df <- ex_df %>%
122+
dplyr::group_by(x) %>%
123+
dplyr::summarise(list_data = list(data), .groups = "drop")
124+
125+
ex_tab <- tab_df %>%
126+
gt::gt() %>%
127+
gt_plt_bar_stack(column = list_data, palette = pal) %>%
128+
gt::as_raw_html() %>%
129+
rvest::read_html()
130+
131+
rect_nodes <- ex_tab %>%
132+
rvest::html_nodes(
133+
"svg > g > g > text:nth-child(4), svg > g > g > text:nth-child(5)"
134+
) %>%
135+
rvest::html_attr("x") %>%
136+
as.double()
137+
expect_gt(length(rect_nodes), 0L)
138+
139+
# Extract attributes
140+
141+
styles <- ex_tab %>%
142+
rvest::html_nodes(
143+
"svg > g > g > text:nth-child(4), svg > g > g > text:nth-child(5)"
144+
) %>%
145+
rvest::html_attr("style")
146+
147+
cols <- normalize_colors(styles)
148+
expect_length(cols, 8L)
149+
})

0 commit comments

Comments
 (0)