Skip to content

Commit 276c5a9

Browse files
authored
Use ellipsis argument in theme_sub_*() family (#6655)
* Comment to clarify when to use `warn_dots_*()` functions * add ellipses to `theme_sub_*()` family * document `...`
1 parent 367ade9 commit 276c5a9

File tree

3 files changed

+84
-13
lines changed

3 files changed

+84
-13
lines changed

R/theme-sub.R

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#' keeping theme declarations more organised.
66
#'
77
#' @eval subtheme_param_doc()
8+
#' @param ... Not in use, expected to be empty.
89
#'
910
#' @return A `theme`-class object that can be added to a plot.
1011
#' @name subtheme
@@ -54,49 +55,57 @@ subtheme <- function(elements, prefix = "", suffix = "", call = caller_env()) {
5455

5556
#' @export
5657
#' @describeIn subtheme Theme specification for all axes.
57-
theme_sub_axis <- function(title, text, ticks, ticks.length, line, minor.ticks.length) {
58+
theme_sub_axis <- function(..., title, text, ticks, ticks.length, line, minor.ticks.length) {
59+
warn_dots_empty()
5860
subtheme(find_args(), "axis.")
5961
}
6062

6163
#' @export
6264
#' @describeIn subtheme Theme specification for both x axes.
63-
theme_sub_axis_x <- function(title, text, ticks, ticks.length, line, minor.ticks.length) {
65+
theme_sub_axis_x <- function(..., title, text, ticks, ticks.length, line, minor.ticks.length) {
66+
warn_dots_empty()
6467
subtheme(find_args(), "axis.", ".x")
6568
}
6669

6770
#' @export
6871
#' @describeIn subtheme Theme specification for both y axes.
69-
theme_sub_axis_y <- function(title, text, ticks, ticks.length, line, minor.ticks.length) {
72+
theme_sub_axis_y <- function(..., title, text, ticks, ticks.length, line, minor.ticks.length) {
73+
warn_dots_empty()
7074
subtheme(find_args(), "axis.", ".y")
7175
}
7276

7377
#' @export
7478
#' @describeIn subtheme Theme specification for the bottom x axis.
75-
theme_sub_axis_bottom <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
79+
theme_sub_axis_bottom <- function(..., title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
80+
warn_dots_empty()
7681
subtheme(find_args(), "axis.", ".x.bottom")
7782
}
7883

7984
#' @export
8085
#' @describeIn subtheme Theme specification for the top x axis.
81-
theme_sub_axis_top <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
86+
theme_sub_axis_top <- function(..., title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
87+
warn_dots_empty()
8288
subtheme(find_args(), "axis.", ".x.top")
8389
}
8490

8591
#' @export
8692
#' @describeIn subtheme Theme specification for the left y axis.
87-
theme_sub_axis_left <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
93+
theme_sub_axis_left <- function(..., title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
94+
warn_dots_empty()
8895
subtheme(find_args(), "axis.", ".y.left")
8996
}
9097

9198
#' @export
9299
#' @describeIn subtheme Theme specification for the right y axis.
93-
theme_sub_axis_right <- function(title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
100+
theme_sub_axis_right <- function(..., title, text, ticks, ticks.length, line, minor.ticks, minor.ticks.length) {
101+
warn_dots_empty()
94102
subtheme(find_args(), "axis.", ".y.right")
95103
}
96104

97105
#' @export
98106
#' @describeIn subtheme Theme specification for the legend.
99107
theme_sub_legend <- function(
108+
...,
100109
# Text stuff
101110
text, text.position, title, title.position,
102111
# Drawn elements
@@ -114,32 +123,36 @@ theme_sub_legend <- function(
114123
# Box
115124
box, box.just, box.margin, box.background, box.spacing
116125
) {
126+
warn_dots_empty()
117127
subtheme(find_args(), "legend.")
118128
}
119129

120130
#' @export
121131
#' @describeIn subtheme Theme specification for the panels.
122-
theme_sub_panel <- function(background, border,
132+
theme_sub_panel <- function(..., background, border,
123133
widths, heights, spacing, spacing.x, spacing.y,
124134
grid, grid.major, grid.minor, grid.major.x,
125135
grid.major.y, grid.minor.x, grid.minor.y, ontop) {
136+
warn_dots_empty()
126137
subtheme(find_args(), "panel.")
127138
}
128139

129140
#' @export
130141
#' @describeIn subtheme Theme specification for the whole plot.
131-
theme_sub_plot <- function(background, title, title.position, subtitle, caption,
142+
theme_sub_plot <- function(..., background, title, title.position, subtitle, caption,
132143
caption.position, tag, tag.position, tag.location,
133144
margin) {
145+
warn_dots_empty()
134146
subtheme(find_args(), "plot.")
135147
}
136148

137149
#' @export
138150
#' @describeIn subtheme Theme specification for facet strips.
139-
theme_sub_strip <- function(background, background.x, background.y, clip,
151+
theme_sub_strip <- function(..., background, background.x, background.y, clip,
140152
placement, text, text.x, text.x.bottom, text.x.top,
141153
text.y, text.y.left, text.y.right,
142154
switch.pad.grid, switch.pad.wrap) {
155+
warn_dots_empty()
143156
subtheme(find_args(), "strip.")
144157
}
145158

@@ -150,6 +163,7 @@ subtheme_param_doc <- function() {
150163
theme_sub_panel, theme_sub_plot, theme_sub_strip
151164
)
152165
args <- sort(unique(unlist(lapply(funs, fn_fmls_names), use.names = FALSE)))
166+
args <- setdiff(args, "...")
153167
paste0(
154168
"@param ",
155169
paste0(args, collapse = ","),

R/utilities.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,36 @@ size0 <- function(x) {
810810
}
811811
}
812812

813+
fallback_palette <- function(scale) {
814+
aes <- scale$aesthetics[1]
815+
discrete <- scale$is_discrete()
816+
if (discrete) {
817+
pal <- switch(
818+
aes,
819+
colour = , fill = pal_hue(),
820+
alpha = function(n) seq(0.1, 1, length.out = n),
821+
linewidth = function(n) seq(2, 6, length.out = n),
822+
linetype = pal_linetype(),
823+
shape = pal_shape(),
824+
size = function(n) sqrt(seq(4, 36, length.out = n)),
825+
ggplot_global$theme_default[[paste0("palette.", aes, ".discrete")]]
826+
)
827+
return(pal)
828+
}
829+
switch(
830+
aes,
831+
colour = , fill = pal_seq_gradient("#132B43", "#56B1F7"),
832+
alpha = pal_rescale(c(0.1, 1)),
833+
linewidth = pal_rescale(c(1, 6)),
834+
linetype = pal_binned(pal_linetype()),
835+
shape = pal_binned(pal_shape()),
836+
size = pal_area(),
837+
ggplot_global$theme_default[[paste0("palette.", aes, ".continuous")]]
838+
)
839+
}
840+
841+
# For when you want to ensure all forwarded arguments are consumed by downstream
842+
# functions.
813843
warn_dots_used <- function(env = caller_env(), call = caller_env()) {
814844
check_dots_used(
815845
env = env, call = call,
@@ -822,6 +852,7 @@ warn_dots_used <- function(env = caller_env(), call = caller_env()) {
822852
)
823853
}
824854

855+
# For when you do not want `...` to be used; it should be empty.
825856
warn_dots_empty <- function(env = caller_env(), call = caller_env()) {
826857
check_dots_empty(
827858
env = env, call = call,

man/subtheme.Rd

Lines changed: 29 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)