@@ -44,7 +44,9 @@ update_labels <- function(p, labels) {
4444# ' @param tag The text for the tag label which will be displayed at the
4545# ' top-left of the plot by default.
4646# ' @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.
4850# ' @param ... A list of new name-value pairs. The name should be an aesthetic.
4951# ' @export
5052# '
@@ -76,7 +78,8 @@ labs <- function(..., title = waiver(), subtitle = waiver(), caption = waiver(),
7678 tag = waiver(), alt = waiver(), alt_insight = waiver()) {
7779 # .ignore_empty = "all" is needed to allow trailing commas, which is NOT a trailing comma for dots_list() as it's in ...
7880 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" )
8083
8184 is_waive <- vapply(args , is.waive , logical (1 ))
8285 args <- args [! is_waive ]
@@ -140,11 +143,15 @@ get_alt_text <- function(p, ...) {
140143}
141144# ' @export
142145get_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
144149}
145150# ' @export
146151get_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
148155}
149156# ' @export
150157get_alt_text.gtable <- function (p , ... ) {
@@ -197,11 +204,16 @@ get_alt_text.gtable <- function(p, ...) {
197204# '
198205generate_alt_text <- function (p ) {
199206 # 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+
205217
206218 # Get axes descriptions
207219 axes <- glue(" showing " , glue_collapse(
@@ -218,7 +230,7 @@ generate_alt_text <- function(p) {
218230 if (length(layers ) == 1 ) " a " else " " ,
219231 glue_collapse(layers , sep = " , " , last = " and " ),
220232 " layer" ,
221- if (length(layers ) == 1 ) " " else " s" ,
233+ if (length(layers ) == 1 ) " " else " s"
222234 )
223235 layers <- safe_string(layers )
224236
0 commit comments