Skip to content

Commit ab39bd2

Browse files
committed
Use if condition to do special for development version of ggplot2 instead of try_fetch
1 parent 240e7d2 commit ab39bd2

File tree

5 files changed

+58
-54
lines changed

5 files changed

+58
-54
lines changed

Diff for: NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,4 @@ importFrom(utils,getFromNamespace)
626626
importFrom(utils,getS3method)
627627
importFrom(utils,modifyList)
628628
importFrom(utils,packageName)
629+
importFrom(utils,packageVersion)

Diff for: R/ggplot-helper.R

+20-24
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ element_polygon <- function(fill = NULL, colour = NULL, linewidth = NULL,
8484
)
8585
}
8686

87+
#' @importFrom utils packageVersion
8788
#' @importFrom grid gpar
8889
#' @importFrom ggplot2 element_grob
8990
#' @export
@@ -94,41 +95,36 @@ element_grob.ggalign_element_polygon <- function(element,
9495
colour = NULL,
9596
linewidth = NULL,
9697
linetype = NULL, ...) {
97-
gp <- try_fetch(
98-
gpar(
98+
if (packageVersion("ggplot2") > "3.5.1") {
99+
gp <- ggfun("gg_par")(
100+
lwd = linewidth, col = colour, fill = fill, lty = linetype
101+
)
102+
element_gp <- ggfun("gg_par")(
103+
lwd = element$linewidth,
104+
col = element$colour,
105+
fill = fill_alpha(element$fill, element$alpha %||% NA),
106+
lty = element$linetype,
107+
lineend = element$lineend,
108+
linejoin = element$linejoin,
109+
linemitre = element$linemitre
110+
)
111+
} else {
112+
gp <- gpar(
99113
lwd = ggfun("len0_null")(linewidth * .pt),
100114
col = colour,
101115
fill = fill,
102116
lty = linetype
103-
),
104-
error = function(cnd) {
105-
ggplot2::gg_par(
106-
lwd = linewidth, col = colour, fill = fill, lty = linetype
107-
)
108-
}
109-
)
110-
element_gp <- try_fetch(
111-
gpar(
117+
)
118+
element_gp <- gpar(
112119
lwd = ggfun("len0_null")(element$linewidth * .pt),
113120
col = element$colour,
114121
fill = fill_alpha(element$fill, element$alpha %||% NA),
115122
lty = element$linetype,
116123
lineend = element$lineend,
117124
linejoin = element$linejoin,
118125
linemitre = element$linemitre
119-
),
120-
error = function(cnd) {
121-
ggplot2::gg_par(
122-
lwd = element$linewidth,
123-
col = element$colour,
124-
fill = fill_alpha(element$fill, element$alpha %||% NA),
125-
lty = element$linetype,
126-
lineend = element$lineend,
127-
linejoin = element$linejoin,
128-
linemitre = element$linemitre
129-
)
130-
}
131-
)
126+
)
127+
}
132128
grid::polygonGrob(
133129
x = x, y = y,
134130
gp = ggfun("modify_list")(element_gp, gp), ...

Diff for: R/ggplot-theme.R

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ theme_no_strip <- function() {
2828
)
2929
}
3030

31+
#' @importFrom utils packageVersion
3132
#' @importFrom rlang try_fetch
3233
complete_theme <- function(theme) {
33-
try_fetch(
34-
ggfun("complete_theme")(theme),
35-
error = function(cnd) ggfun("plot_theme")(list(theme = theme))
36-
)
34+
if (packageVersion("ggplot2") > "3.5.1") {
35+
ggfun("complete_theme")(theme)
36+
} else {
37+
ggfun("plot_theme")(list(theme = theme))
38+
}
3739
}
3840

3941
#' @importFrom ggplot2 register_theme_elements el_def element_line

Diff for: R/ggplot-utils.R

+6-8
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ allow_lambda <- function(x) {
1313
if (rlang::is_formula(x)) rlang::as_function(x) else x
1414
}
1515

16+
#' @importFrom utils packageVersion
1617
#' @importFrom rlang try_fetch
1718
fill_alpha <- function(colour, alpha = NA) {
18-
try_fetch(
19-
# for version >= 3.5.0
20-
ggplot2::fill_alpha(colour, alpha),
21-
error = function(cnd) {
22-
# for version < 3.5.0
23-
ggplot2::alpha(colour, alpha)
24-
}
25-
)
19+
if (packageVersion("ggplot2") >= "3.5.0") {
20+
ggplot2::fill_alpha(colour, alpha)
21+
} else {
22+
ggplot2::alpha(colour, alpha)
23+
}
2624
}
2725

2826
is.waive <- function(x) inherits(x, "waiver")

Diff for: R/layout-circle-build.R

+25-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ggalign_build.CircleLayout <- function(x) {
44
circle_build(x)
55
}
66

7+
#' @importFrom utils packageVersion
78
#' @importFrom ggplot2 find_panel calc_element ggproto ggplotGrob theme
89
#' @importFrom gtable gtable_add_grob gtable_add_padding
910
#' @importFrom grid unit viewport editGrob
@@ -170,29 +171,35 @@ circle_build <- function(circle, schemes = NULL, theme = NULL) {
170171
}
171172

172173
# build legends
173-
default_position <- plot_theme$legend.position %||% "right"
174-
if (length(default_position) == 2) {
175-
default_position <- "inside"
176-
}
177-
if (!identical(default_position, "none")) {
178-
plot_theme$legend.key.width <- calc_element(
179-
"legend.key.width",
180-
plot_theme
181-
)
182-
plot_theme$legend.key.height <- calc_element(
183-
"legend.key.height",
184-
plot_theme
185-
)
186-
guides[[i]] <- plot$guides$draw(
187-
plot_theme,
188-
default_position,
189-
plot_theme$legend.direction
190-
)
174+
if (packageVersion("ggplot2") > "3.5.1") {
175+
# ggplot2 development version > 3.5.1
176+
guides[[i]] <- plot$guides$assemble(plot_theme)
177+
} else {
178+
default_position <- plot_theme$legend.position %||% "right"
179+
if (length(default_position) == 2) {
180+
default_position <- "inside"
181+
}
182+
if (!identical(default_position, "none")) {
183+
plot_theme$legend.key.width <- calc_element(
184+
"legend.key.width",
185+
plot_theme
186+
)
187+
plot_theme$legend.key.height <- calc_element(
188+
"legend.key.height",
189+
plot_theme
190+
)
191+
guides[[i]] <- plot$guides$draw(
192+
plot_theme,
193+
default_position,
194+
plot_theme$legend.direction
195+
)
196+
}
191197
}
192198
origin <- just
193199
last_plot_size <- plot_size # the last plot panel size
194200
last_spacing <- spacing
195201
}
202+
196203
# attach the guide legends
197204
guides <- lapply(c(.TLBR, "inside"), function(guide_pos) {
198205
unlist(lapply(guides, .subset2, guide_pos), FALSE, FALSE)

0 commit comments

Comments
 (0)