|
1 | | -# Copyright 2021 BPCells contributors |
| 1 | +# Copyright 2025 BPCells contributors |
2 | 2 | # |
3 | 3 | # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
4 | 4 | # https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
|
16 | 16 | # skip over during error printing. |
17 | 17 |
|
18 | 18 | argument_name <- function(arg, n) { |
| 19 | + substitute_if_present <- function(expr, env) { |
| 20 | + if (is.null(env) || !is.symbol(expr)) return(expr) |
| 21 | + symbol <- as.character(expr) |
| 22 | + if (!exists(symbol, envir = env, inherits = FALSE)) return(expr) |
| 23 | + result <- do.call(substitute, list(expr, env)) |
| 24 | + if (!is.language(result)) return(expr) |
| 25 | + result |
| 26 | + } |
| 27 | + |
19 | 28 | arg_name <- substitute(arg) |
20 | | - for (f in seq_len(n)) { |
21 | | - arg_name <- do.call(substitute, list(arg_name, parent.frame(f))) |
| 29 | + if (n > 0) { |
| 30 | + for (f in seq_len(n)) { |
| 31 | + env <- parent.frame(f) |
| 32 | + arg_name <- substitute_if_present(arg_name, env) |
| 33 | + } |
22 | 34 | } |
23 | | - arg_name <- deparse(arg_name) |
| 35 | + |
| 36 | + # Continue walking up the call stack so wrappers (e.g., pkgdown example helpers) |
| 37 | + # don't collapse names to temporary argument placeholders. |
| 38 | + frame <- n |
| 39 | + extra_steps <- 0 |
| 40 | + max_extra <- 200 |
| 41 | + while (extra_steps < max_extra) { |
| 42 | + frame <- frame + 1 |
| 43 | + env <- parent.frame(frame) |
| 44 | + arg_name_new <- substitute_if_present(arg_name, env) |
| 45 | + if (identical(arg_name_new, arg_name)) { |
| 46 | + if (identical(env, globalenv()) || identical(env, baseenv()) || identical(env, emptyenv())) break |
| 47 | + extra_steps <- extra_steps + 1 |
| 48 | + next |
| 49 | + } |
| 50 | + arg_name <- arg_name_new |
| 51 | + if (identical(env, globalenv()) || identical(env, baseenv()) || identical(env, emptyenv())) break |
| 52 | + extra_steps <- extra_steps + 1 |
| 53 | + } |
| 54 | + |
| 55 | + # `deparse()` can return multi-line output (length > 1) for large expressions. |
| 56 | + # Collapse to a single string so downstream consumers that expect a scalar |
| 57 | + # name (e.g., column labels) don't get tripped up by long vectors. |
| 58 | + arg_name <- paste(deparse(arg_name), collapse = "") |
24 | 59 | return(arg_name) |
25 | 60 | } |
26 | 61 |
|
|
0 commit comments