R语言 如何将所有参数连同它们的值一起传递给函数?

7jmck4yq  于 2023-03-15  发布在  其他
关注(0)|答案(4)|浏览(158)

我有一个功能

adebo.deepSearch = function(z, pi_0 = 0.3, families=list(), ... )
    {

    }

我想捕获通过一个名为grabFunctionParameters的函数传入的所有参数名和值;例如,

adebo.deepSearch = function(z, pi_0 = 0.3, families=list(), ... )
    {
    args = grabFunctionParameters();
    }

其中args是一个包含“键”和“值”的列表,例如
args[["pi_0"] = 0.3;
对于所有键和值,包括省略号(...)中的键和值。

一个理想的(可变)解决方案是外部函数grabFunctionParameters()

溶液:

以下是提供的“接受的答案”:

# https://stackoverflow.com/questions/66329835/
# nice work :: B. Christian Kamgang
# .GlobalEnv$.function.args.memory ... key memory on last function call ... so I could reference outside the function
grabFunctionParameters <- function() {
    pf <- parent.frame()    
    args_names <- ls(envir = pf, all.names = TRUE, sorted = FALSE)
    if("..." %in% args_names) {
    dots <- eval(quote(list(...)), envir = pf)
    }  else {
    dots = list()
    }
    args_names <- sapply(setdiff(args_names, "..."), as.name)
    if(length(args_names)) {
    not_dots <- lapply(args_names, eval, envir = pf) 
    } else {
    not_dots <- list()
    }   
   idx <- names(dots) != "";
   list(.keys. = names(not_dots), .vals. = unname(not_dots), .fn. = as.character(sys.call(1L)[[1L]]), .scope. = pf, .dot.keys. = names(dots[idx]), .dot.vals. = unname(dots[idx])); 
}

以下是提供的已接受答案(格式略有不同):

grabFunctionParameters <- function() 
    {
    pf          = parent.frame();    
    my.names    = ls(envir = pf, all.names = TRUE, sorted = FALSE);
    
    dots        = if("..." %in% my.names) { eval(quote(list(...)), envir = pf); } else { list(); }  
    dots.idx    = ( names(dots) != "" );
    
    remaining   = sapply( setdiff(my.names, "..."), as.name);
    
    not.dots    = if(length(remaining) > 0) { lapply( remaining, eval, envir = pf);  } else { list(); }
    
   
    res = list();
    
        res$.fn.            = as.character( sys.call(1L)[[1L]] );
        res$.scope.         = pf;
        res$.keys.          = names( not.dots );
        res$.vals.          = not.dots;                             # unname(not_dots);  # I want keys on "vals"
        res$.dots.keys.     = names( dots[dots.idx] );
        res$.dots.vals.     = dots[dots.idx];                       # unname(dots[dots.idx]); 

    res;
    }
plicqrtu

plicqrtu1#

这里有一个可能的解决方案。这个解决方案要求函数参数不指定默认值(如下面的z)。

grabFunctionParameters <- function() {
  pf <- parent.frame()                                   # get caller environment
  dots <- eval(quote(list(...)), envir = pf)             # get ... in the caller
  nms <- sapply(ls(envir = pf, sorted = FALSE), as.name) # get argument names different from names in ... in the caller
  out <- c(lapply(nms, eval, envir = pf), dots)          # get all arguments/values
  out[names(out) != ""]                                  # remove unnamed values in ... (if any)
}

用例示例

adebo.deepSearch = function(z, pi_0 = 0.3, families=list(), ... ) {
  args = grabFunctionParameters();
  args
}

一些情景

adebo.deepSearch(z=4)
# $z
# [1] 4
# 
# $pi_0
# [1] 0.3
# 
# $families
# list()
# 
adebo.deepSearch(z=4, pi_0=9, families = list(z=1:2))  
# $z
# [1] 4
# 
# $pi_0
# [1] 9
# 
# $families
# $families$z
# [1] 1 2
# 
# 
adebo.deepSearch(z=4, pi_0=9, ac=5, bc=6)  # some additional arguments for ...
# $z
# [1] 4
# 
# $pi_0
# [1] 9
# 
# $families
# list()
# 
# $ac
# [1] 5
# 
# $bc
# [1] 6

Udapte:这是上面函数的更新,使其更通用。

它总是返回一个列表:

  • 如果调用者(函数)没有参数(或者只有...未命名的值),则为空列表。
  • 形式参数名(不在...中)可以以点开头。前一个函数要求调用者具有...;和调用方的形参名称以点(而不是...)开头。

新功能

grabFunctionParameters <- function() {
    pf <- parent.frame()    
    args_names <- ls(envir = pf, all.names = TRUE, sorted = FALSE)
    if("..." %in% args_names) {
    dots <- eval(quote(list(...)), envir = pf)
    }  else {
    dots = list()
    }
    args_names <- sapply(setdiff(args_names, "..."), as.name)
    if(length(args_names)) {
    not_dots <- lapply(args_names, eval, envir = pf) 
    } else {
    not_dots <- list()
    }
    out <- c(not_dots, dots)
    out[names(out) != ""]                                  # remove unnamed values in ... (if any)
}

一些情景

fn1 <- function() grabFunctionParameters()                              # the initial function (before the update) required ... argument
fn2 <- function(x=1, .a=2, b=list(), ...) grabFunctionParameters()      # the initial function did not return .a 
fn3 <- function(.x, .a=2, b=list(), ...) grabFunctionParameters()
fn4 <- function(...) grabFunctionParameters()
fn5 <- function(x, .a) grabFunctionParameters()                        # the initial function required ... argument

fn1()     # correct since the caller has no argument. Previously not allowed!
# list()

fn2()
# $x
# [1] 1
# 
# $.a
# [1] 2
# 
# $b
# list()
                                    
fn2(.a=10, ac=4, bc=7, .xy=1)      #    
# $x
# [1] 1
# 
# $.a
# [1] 10
# 
# $b
# list()
# 
# $ac
# [1] 4
# 
# $bc
# [1] 7
# 
# $.xy
# [1] 1

fn3(10)
# $.x
# [1] 10
# 
# $.a
# [1] 2
# 
# $b
# list()

fn3()       # throw an error! (.x required!). This will not happen if we use mget function and not lapply/supply inside grabFunctionParameters above. 
# Error in FUN(X[[i]], ...) : argument ".x" is missing, with no default

fn4(a = 5, b = 6, c = 6, 6, 7, 9)       # unnamed values are dropped
# $a
# [1] 5
# 
# $b
# [1] 6
# 
# $c
# [1] 6

fn5(6, 8)
# $x
# [1] 6
# 
# $.a
# [1] 8
u5i3ibmn

u5i3ibmn2#

您可以mget函数环境。

adebo.deepSearch <- function(z, pi_0 = 0.3, families=list(), ... ) {
  c(mget(ls(environment(), sorted=F)), match.call(expand.dots=F)$...)
}
adebo.deepSearch(foo=1, z=2)
# $z
# [1] 2
# 
# $pi_0
# [1] 0.3
# 
# $families
# list()
# 
# $foo
# [1] 1
cx6n0qe3

cx6n0qe33#

adebo <- function(z, pi_0 = 0.3, families = list(), ...) {
  args <- formals(adebo)
  return(args)
}

adebo()
#> $z
#> 
#> 
#> $pi_0
#> [1] 0.3
#> 
#> $families
#> list()
#> 
#> $...

reprex package(v1.0.0)于2021年2月23日创建

uxh89sit

uxh89sit4#

不是解决方案,而是捕获“...”部分的想法。以引号或引号列表的形式返回为...传递的参数。

adebo <- function(z, pi_0 = 0.3, families = list(), ...) {
  rlang::enquos(...)
}

adebo(trash = "trash", idea = "idea")
#> <list_of<quosure>>
#> 
#> $trash
#> <quosure>
#> expr: ^"trash"
#> env:  empty
#> 
#> $idea
#> <quosure>
#> expr: ^"idea"
#> env:  empty

reprex package(v1.0.0)于2021年2月23日创建

相关问题