[R] obtain names & values of all arguments passed to a function including args passed using the "three dots" ellipsis ( ...) parameter, for use in do.call

Kelly Thompson kt1572757 @end|ng |rom gm@||@com
Sat Mar 12 22:29:58 CET 2022


#What are "good" ways to obtain all arguments passed to or being used
by a function, including arguments passed to the function using the
"three dots" ellipsis ( ...) parameter, so that these arguments can be
used in the args parameter of do.call(what, args) .

#I think I've figured out a method that uses environment() and
match.call() (see example below).

#I'm looking for feedback about problems with my approach, and
suggestions about better ways to do this.

#Thank you!

#Related posts
#https://stackoverflow.com/questions/51259346/how-to-get-names-of-dot-dot-dot-arguments-in-r
#https://stackoverflow.com/questions/35317587/extract-names-of-dataframes-passed-with-dots
#https://stackoverflow.com/questions/17256834/getting-the-arguments-of-a-parent-function-in-r-with-names

#Example

#Note: "udf_" indicates this is a "user-defined function"

#start udf_return_all_arguments
    udf_return_all_arguments <- function (...)  {

#obtain all arguments passed to or being used by a function, including
arguments passed to the function using the "three dots" ellipsis (
...) parameter
#
#this obtains the arguments for parameters explicitly listed in the function
        env <- as.list( environment() )

#this obtains the arguments for parameters NOT explicitly listed in
the function and included    using the 3 dots ellipses ( ... )
parameters feature
        mc <- match.call(expand.dots = FALSE)
        dots <- as.list( mc$...)

#these are all the arguments from the function
        args_in <-  c(env, dots)

        return(args_in)

    } # end udf_return_all_arguments

################################################################

#start udf_pass_all_argumens_to_do_call
    udf_pass_all_argumens_to_do_call <- function(x, y, z = NULL, na.rm
= T, FUN, ... ) {

#obtain all arguments passed to or being used by a function, including
arguments passed to the function using the "three dots" ellipsis (
...) parameter
#
#this obtains the arguments for parameters explicitly listed in the function
        env <- as.list( environment() )

#this obtains the arguments for parameters NOT explicitly listed in
the function and included using the 3 dots ellipses ( ... ) parameters
feature
        mc <- match.call(expand.dots = FALSE)
        dots <- as.list( mc$...)

#these are all the arguments from the function
        args_in <-  c(env, dots)

        do.call( what = FUN, args = args_in)

    } #end udf_pass_all_argumens_to_do_call

    my_list_all_args <- udf_pass_all_argumens_to_do_call(x = 123, y =
456, FUN = udf_return_all_arguments, non_specified_paramter = "this is
an example")
    my_list_all_args
### end of example



More information about the R-help mailing list