【问题标题】:How to capture the names of multiple arguments (...) passed to a function?如何捕获传递给函数的多个参数(...)的名称?
【发布时间】:2021-01-13 13:37:27
【问题描述】:

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

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 创建

【问题讨论】:

    标签: r tidyverse tidymodels


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多