@@ -44,7 +44,9 @@ update_labels <- function(p, labels) {
44
44
# ' @param tag The text for the tag label which will be displayed at the
45
45
# ' top-left of the plot by default.
46
46
# ' @param alt,alt_insight Text used for the generation of alt-text for the plot.
47
- # ' See [get_alt_text] for examples.
47
+ # ' See [get_alt_text] for examples. `alt` can also be a function that
48
+ # ' takes the plot as input and returns text as output. `alt` also accepts
49
+ # ' rlang [lambda][rlang::as_function()] function notation.
48
50
# ' @param ... A list of new name-value pairs. The name should be an aesthetic.
49
51
# ' @export
50
52
# '
@@ -76,7 +78,8 @@ labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(),
76
78
tag = waiver(), alt = waiver(), alt_insight = waiver()) {
77
79
# .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
78
80
args <- dots_list(... , title = title , subtitle = subtitle , caption = caption ,
79
- tag = tag , alt = alt , alt_insight = alt_insight , .ignore_empty = " all" )
81
+ tag = tag , alt = allow_lambda(alt ), alt_insight = alt_insight ,
82
+ .ignore_empty = " all" )
80
83
81
84
is_waive <- vapply(args , is.waive , logical (1 ))
82
85
args <- args [! is_waive ]
@@ -140,11 +143,15 @@ get_alt_text <- function(p, ...) {
140
143
}
141
144
# ' @export
142
145
get_alt_text.ggplot <- function (p , ... ) {
143
- p $ labels [[" alt" ]] %|| % " "
146
+ alt <- p $ labels [[" alt" ]] %|| % " "
147
+ p $ labels [[" alt" ]] <- NULL
148
+ if (is.function(alt )) alt(p ) else alt
144
149
}
145
150
# ' @export
146
151
get_alt_text.ggplot_built <- function (p , ... ) {
147
- p $ plot $ labels [[" alt" ]] %|| % " "
152
+ alt <- p $ plot $ labels [[" alt" ]] %|| % " "
153
+ p $ plot $ labels [[" alt" ]] <- NULL
154
+ if (is.function(alt )) alt(p $ plot ) else alt
148
155
}
149
156
# ' @export
150
157
get_alt_text.gtable <- function (p , ... ) {
@@ -197,11 +204,16 @@ get_alt_text.gtable <- function(p, ...) {
197
204
# '
198
205
generate_alt_text <- function (p ) {
199
206
# Combine titles
200
- title <- glue(glue_collapse(
201
- sub(" \\ .?$" , " " , c(p $ labels $ title , p $ labels $ subtitle )),
202
- last = " : "
203
- ), " . " )
204
- title <- safe_string(title )
207
+ if (! is.null(p $ label $ title %|| % p $ labels $ subtitle )) {
208
+ title <- glue(glue_collapse(
209
+ sub(" \\ .?$" , " " , c(p $ labels $ title , p $ labels $ subtitle )),
210
+ last = " : "
211
+ ), " . " )
212
+ title <- safe_string(title )
213
+ } else {
214
+ title <- " "
215
+ }
216
+
205
217
206
218
# Get axes descriptions
207
219
axes <- glue(" showing " , glue_collapse(
@@ -218,7 +230,7 @@ generate_alt_text <- function(p) {
218
230
if (length(layers ) == 1 ) " a " else " " ,
219
231
glue_collapse(layers , sep = " , " , last = " and " ),
220
232
" layer" ,
221
- if (length(layers ) == 1 ) " " else " s" ,
233
+ if (length(layers ) == 1 ) " " else " s"
222
234
)
223
235
layers <- safe_string(layers )
224
236
0 commit comments