使用 rlang::fn_fmls 修改后如何更新函数签名?

How to update function signature after modification with rlang::fn_fmls?

rlang::fn_fmls<- 允许我们修改函数签名:

f <- function(a, b, c) print(paste(a, c))
f_args <- rlang::fn_fmls(f)
f_args["b"] <- NULL
rlang::fn_fmls(f) <- f_args
f(1, 2)
#> [1] "1 2"

reprex package (v0.3.0)

于 2020-04-07 创建

很明显,我们已经从函数的签名中删除了 b。但是,如果我们打印函数本身:

f
#> function(a, b, c) print(paste(a, c))

b 仍然出现在它的签名中。使用 args(f)(仅打印签名)实际上有效并且省略了 b

我应该怎么做才能使函数(带正文)正确打印,省略 b

问题似乎是 print.function()useSource = TRUE,它强制使用原始函数签名和函数体。

我找到的解决方案是修改函数的 class 并创建自定义打印函数。

f <- function(a, b, c) {
  b <- 1
  print(paste(a, c))
}
f_args <- rlang::fn_fmls(f)
f_args["b"] <- NULL
rlang::fn_fmls(f) <- f_args
class(f) <- c("myfunction", "function")

print.myfunction <- function(f) {
  class(f) <- NULL
  print(f, useSource = FALSE)
}

f
#> function (a, c) 
#> {
#>     b <- 1
#>     print(paste(a, c))
#> }

reprex package (v0.3.0)

于 2020-04-07 创建