【问题标题】:Get argument name instead of value in a function in R [duplicate]在R中的函数中获取参数名称而不是值[重复]
【发布时间】:2023-03-05 05:33:01
【问题描述】:

我正在尝试找到一种在函数中使用向量名称而不是其值的方法。

a_vector <- 1:20
testFun <- function(x = a_vector) {
# Function should return "a_vector" and not 1:20
}

这类似于 plot() 默认为 xlab 使用的 xlab,它使用 x 的描述。

非常感谢您的帮助。

【问题讨论】:

    标签: r


    【解决方案1】:

    我们可以使用deparse/substitute

    testFun <- function(x = a_vector) {
           deparse(substitute(x))
       }
    
    testFun()
    #[1] "a_vector"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-24
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      相关资源
      最近更新 更多