如何捕获传递给函数的多个参数(...)的名称?

How to capture the names of multiple arguments (...) passed to a function?

我有几个由函数传递的模型,模型的数量是可变的。通过 (...) 我可以通过以下所有模型:

library(tidymodels)
library(dplyr)

model_1 = decision_tree(mode = "regression") %>%  set_engine("rpart") %>% fit(Sepal.Length ~ Petal.Length, data = iris)
model_2 = decision_tree(mode = "regression") %>%  set_engine("rpart") %>% fit(Sepal.Length ~ Petal.Length + Petal.Width, data = iris)

F.function = function(...){
  
  a = list(...)
  a
  
}

F.function(model_1,model_2)
#> [[1]]
#> parsnip model object
#> 
#> Fit time:  15ms 
#> n= 150 
#> 
#> node), split, n, deviance, yval
#>       * denotes terminal node
#> 
#>  1) root 150 102.1683000 5.843333  
#>    2) Petal.Length< 4.25 73  13.1391800 5.179452  
#>      4) Petal.Length< 3.4 53   6.1083020 5.005660 *
#>      5) Petal.Length>=3.4 20   1.1880000 5.640000 *
#>    3) Petal.Length>=4.25 77  26.3527300 6.472727  
#>      6) Petal.Length< 6.05 68  13.4923500 6.326471  
#>       12) Petal.Length< 5.15 43   8.2576740 6.165116 *
#>       13) Petal.Length>=5.15 25   2.1896000 6.604000 *
#>      7) Petal.Length>=6.05 9   0.4155556 7.577778 *
#> 
#> [[2]]
#> parsnip model object
#> 
#> Fit time:  5ms 
#> n= 150 
#> 
#> node), split, n, deviance, yval
#>       * denotes terminal node
#> 
#>  1) root 150 102.1683000 5.843333  
#>    2) Petal.Length< 4.25 73  13.1391800 5.179452  
#>      4) Petal.Length< 3.4 53   6.1083020 5.005660 *
#>      5) Petal.Length>=3.4 20   1.1880000 5.640000 *
#>    3) Petal.Length>=4.25 77  26.3527300 6.472727  
#>      6) Petal.Length< 6.05 68  13.4923500 6.326471  
#>       12) Petal.Length< 5.15 43   8.2576740 6.165116 *
#>       13) Petal.Length>=5.15 25   2.1896000 6.604000 *
#>      7) Petal.Length>=6.05 9   0.4155556 7.577778 *

但是当模型保存在列表中时,它们不保留名称,有没有办法让以下输出使用 (...) 存储名称?

list(model_1 = model_1,
     model_2 = model_2)
#> $model_1
#> parsnip model object
#> 
#> Fit time:  15ms 
#> n= 150 
#> 
#> node), split, n, deviance, yval
#>       * denotes terminal node
#> 
#>  1) root 150 102.1683000 5.843333  
#>    2) Petal.Length< 4.25 73  13.1391800 5.179452  
#>      4) Petal.Length< 3.4 53   6.1083020 5.005660 *
#>      5) Petal.Length>=3.4 20   1.1880000 5.640000 *
#>    3) Petal.Length>=4.25 77  26.3527300 6.472727  
#>      6) Petal.Length< 6.05 68  13.4923500 6.326471  
#>       12) Petal.Length< 5.15 43   8.2576740 6.165116 *
#>       13) Petal.Length>=5.15 25   2.1896000 6.604000 *
#>      7) Petal.Length>=6.05 9   0.4155556 7.577778 *
#> 
#> $model_2
#> parsnip model object
#> 
#> Fit time:  5ms 
#> n= 150 
#> 
#> node), split, n, deviance, yval
#>       * denotes terminal node
#> 
#>  1) root 150 102.1683000 5.843333  
#>    2) Petal.Length< 4.25 73  13.1391800 5.179452  
#>      4) Petal.Length< 3.4 53   6.1083020 5.005660 *
#>      5) Petal.Length>=3.4 20   1.1880000 5.640000 *
#>    3) Petal.Length>=4.25 77  26.3527300 6.472727  
#>      6) Petal.Length< 6.05 68  13.4923500 6.326471  
#>       12) Petal.Length< 5.15 43   8.2576740 6.165116 *
#>       13) Petal.Length>=5.15 25   2.1896000 6.604000 *
#>      7) Petal.Length>=6.05 9   0.4155556 7.577778 *

由 reprex 包 (v0.3.0) 创建于 2021-01-13

您没有为这些参数使用任何名称。如果你有,那些将是结果的一部分。例如,使用

F.function(m1 = model_1,m2 = model_2)

如果您想从传入的表达式中生成名称,您可以这样做:

exprs <- substitute(list(...))
vals <- list(...)
names(vals) <- vapply(as.list(exprs), deparse, "")[-1]

此时 vals 将包含传入 ... 的参数,名称设置为用于指定它们的表达式。