Skip to content

Commit ad3d5ce

Browse files
authored
Merge pull request #277 from tidymodels/check-for-pipe
add error for piping after visualize()
2 parents be264a0 + 00ddd99 commit ad3d5ce

5 files changed

+60
-0
lines changed

R/shade_confidence_interval.R

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ NULL
7070
#' @export
7171
shade_confidence_interval <- function(endpoints, color = "mediumaquamarine",
7272
fill = "turquoise", ...) {
73+
# argument checking
74+
check_for_piped_visualize(endpoints, color, fill)
75+
7376
dots <- list(...)
7477

7578
endpoints <- impute_endpoints(endpoints)

R/shade_p_value.R

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ NULL
6060
#' @export
6161
shade_p_value <- function(obs_stat, direction,
6262
color = "red2", fill = "pink", ...) {
63+
# argument checking
64+
check_for_piped_visualize(obs_stat, direction, color, fill)
6365
obs_stat <- check_obs_stat(obs_stat)
6466
check_shade_p_value_args(obs_stat, direction, color, fill)
6567

R/utils.R

+24
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ check_direction <- function(direction = c("less", "greater", "two_sided",
367367

368368
check_obs_stat <- function(obs_stat) {
369369
if (!is.null(obs_stat)) {
370+
370371
if ("data.frame" %in% class(obs_stat)) {
371372
check_type(obs_stat, is.data.frame)
372373
if ((nrow(obs_stat) != 1) || (ncol(obs_stat) != 1)) {
@@ -439,3 +440,26 @@ parse_type <- function(f_name) {
439440
regexec("is[_\\.]([[:alnum:]_\\.]+)$", f_name)
440441
)[[1]][2]
441442
}
443+
444+
# Helpers for visualize() Utilities -----------------------------------------------
445+
446+
# a function for checking arguments to functions that are added as layers
447+
# to visualize()d objects to make sure they weren't mistakenly piped
448+
check_for_piped_visualize <- function(...) {
449+
450+
is_ggplot_output <- vapply(list(...), ggplot2::is.ggplot, logical(1))
451+
452+
if (any(is_ggplot_output)) {
453+
454+
called_function <- sys.call(-1)[[1]]
455+
456+
stop_glue(
457+
"It looks like you piped the result of `visualize()` into ",
458+
"`{called_function}()` (using `%>%`) rather than adding the result of ",
459+
"`{called_function}()` as a layer with `+`. Consider changing",
460+
"`%>%` to `+`."
461+
)
462+
}
463+
464+
TRUE
465+
}

tests/testthat/test-shade_confidence_interval.R

+16
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ test_that("shade_confidence_interval throws errors and warnings", {
6868
iris_viz_sim + shade_confidence_interval(c(-1, 1), fill = "x"),
6969
"color"
7070
)
71+
expect_error(
72+
iris_viz_sim %>% shade_confidence_interval(c(-1, 1)),
73+
"\\`shade_confidence_interval\\(\\)\\` as a layer"
74+
)
75+
expect_error(
76+
iris_viz_sim %>% shade_confidence_interval(endpoints = c(-1, 1)),
77+
"\\`shade_confidence_interval\\(\\)\\` as a layer"
78+
)
79+
expect_error(
80+
iris_viz_sim %>% shade_ci(c(-1, 1)),
81+
"\\`shade_ci\\(\\)\\` as a layer"
82+
)
83+
expect_error(
84+
iris_viz_sim %>% shade_ci(endpoints = c(-1, 1)),
85+
"\\`shade_ci\\(\\)\\` as a layer"
86+
)
7187
})
7288

7389

tests/testthat/test-shade_p_value.R

+15
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ test_that("shade_p_value throws errors", {
9292
expect_error(iris_viz_sim + shade_p_value(1, 1), "character")
9393
expect_error(iris_viz_sim + shade_p_value(1, "right", color = "x"), "color")
9494
expect_error(iris_viz_sim + shade_p_value(1, "right", fill = "x"), "color")
95+
expect_error(iris_viz_sim %>% shade_p_value(1, "right"),
96+
"\\`shade_p_value\\(\\)\\` as a layer")
97+
expect_error(iris_viz_sim %>% shade_p_value(obs_stat = 1),
98+
"\\`shade_p_value\\(\\)\\` as a layer")
99+
expect_error(iris_viz_sim %>% shade_p_value(obs_stat = 1,
100+
direction = "right"),
101+
"\\`shade_p_value\\(\\)\\` as a layer")
102+
expect_error(iris_viz_sim %>% shade_pvalue(1, "right"),
103+
"\\`shade_pvalue\\(\\)\\` as a layer")
104+
expect_error(iris_viz_sim %>% shade_pvalue(obs_stat = 1),
105+
"\\`shade_pvalue\\(\\)\\` as a layer")
106+
expect_error(iris_viz_sim %>% shade_pvalue(obs_stat = 1,
107+
direction = "right"),
108+
"\\`shade_pvalue\\(\\)\\` as a layer")
109+
95110
})
96111

97112

0 commit comments

Comments
 (0)