【问题标题】:Print arguments of a function in R在 R 中打印函数的参数
【发布时间】:2016-02-26 00:30:40
【问题描述】:

是否可以打印函数的参数?

例如,这是我的函数:

my_function <- function(argument_1, argument_2){
            my_equation <- argument_1 + argument_2
            return(my_equation)
}

我用这段代码运行它:

my_save <- my_function(argument_1=1, argument_2=123)

是否可以编写类似GET.MY.FUNCTION.PARAMETERS(my_save) 的东西,它会返回带有1, 123 的向量或列表

【问题讨论】:

  • 您也可以查看match.call,如果函数m &lt;- match.call(); m$argument_1 中的参数发生变化,您可以索引任何参数

标签: r function parameters arguments


【解决方案1】:

一种快速的方法是将return 中的参数作为列表的一部分传回:

my_function <- function(argument_1, argument_2){
        my_equation <- argument_1 + argument_2
        return(list(eqn = my_equation, arg1 = argument_1, arg2 = argument_2))
}

这样你就可以分配一个输出:

results <- my_function(foo1, foo2)

然后剥离你需要的信息:

eqn <- results$eqn
args <- with(results, c(arg1, arg2))
print(args)

【讨论】:

    猜你喜欢
    • 2014-07-14
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    相关资源
    最近更新 更多