简体   繁体   中英

Print arguments of a function in R

Is it possible to print the arguments of a function?

Example, this is my function:

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

And i run it with this code:

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

Is it possible to write something like this GET.MY.FUNCTION.PARAMETERS(my_save) which would return a vector or list with 1, 123

A quick way to do this is to pass the arguments back in return as part of a list:

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

So that you can assign an output:

results <- my_function(foo1, foo2)

and then strip the information you need:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM