【问题标题】:Passing default arguments through nested functions in R通过 R 中的嵌套函数传递默认参数
【发布时间】:2018-11-03 03:04:35
【问题描述】:

我有一个关于通过 R 中的嵌套函数传递默认参数的一般性问题。我的示例(显然)是对我实际想要做的事情的极大简化,但我在其他时候遇到了问题,所以我正在寻找一个通用的解决方案。

基本上我有三个函数,并且希望某些元素在所有三个函数中都具有默认值。这些函数相互调用,但有时单独调用(即foo1 调用foo2foo2 调用foo3,但有时我只调用foo3foo2)。

编辑澄清:我正在寻找一种方法来传递wd(在我的真正问题中)一些其他变量从被调用的函数级别通过,而不必在每个函数调用中写出它们。那就是我想在所有级别设置默认的wd。为了避免将它们命名为所有不同的东西,如果你想单独调用我尝试在每个函数调用中使用wd = wd 的函数(旨在从它正在调用的函数访问wd。这给了promise already under evaluation: recursive default argument reference or earlier problems? . 有没有更好的方法来传递参数,以便在另一个函数中调用时传递参数?

我的第一个示例作品显示了工作代码,但是每次我在一个内调用其他函数时,我都必须指定 wd = wd,当它是多个参数时,我在 @ 内多次调用函数987654335@ 使脚本难以使用。

第二个示例是我所希望的,其中wd 在调用它的环境中默认设置为wd,但这显然不允许发生错误。在不必不断分配参数的情况下传递参数的最佳方法是什么?

编辑:我查看了promise already under evaluation: recursive default argument reference or earlier problems? 的答案,我知道这是一种可能的解决方法(它是我当前运行脚本的方式),但我真的希望参数名称相同每个函数的名称,以便在单独调用它们时可以快速轻松地记住参数名称。

示例 1:工作正常,但未设置默认值。

foo1 <- function(x, wd, reps){

  filename = paste0("Event", x)
  myString <- foo2(file = filename, wd = wd)
  rep(myString, reps)
}

foo2 <- function(file,wd){
  filePath <- file.path(wd,file)
  foo3(Path = filePath, wd = wd)
}

foo3 <- function(Path, wd){
  paste("Absolute File path is:", Path, "Current working Dir is:", wd)
}

foo1(1, wd = "c:/temp", 2)
foo2("C:/temp/otherfilename")

示例 2:

foo2 <- function(file,wd){
  filePath <- file.path(wd,file)
  foo3(Path = filePath)
}

foo3 <- function(Path, wd=wd){
  paste("Absolute File path is:", Path, "Current working Dir is:", wd)
}
foo1(1, wd = "c:/temp", 2)
> Error in paste("Absolute File path is:", Path, "Current working Dir is:",  : 
         promise already under evaluation: recursive default argument reference or earlier problems? 

【问题讨论】:

  • 函数定义foo3 &lt;- function(Path, wd=wd){不正确。像foo3 &lt;- function(Path, wd="string"){ 这样的东西应该没问题。
  • 但是我不能自动将foo2 中的wd 传递到foo3,这是我希望实现的目标
  • 苏仁是正确的,定义是错误的。当然,即使 foo3 有默认参数,你仍然可以自动将参数从 foo2 传递给 foo3。
  • 我知道wd = wd 不起作用。我的问题是传递参数的最佳方法是什么,你想通过它传递给所有嵌套函数,而不必在更高级别函数的每个函数调用中定义它们?

标签: r function nested parameter-passing


【解决方案1】:

我认为您不应该尝试在不同的环境中重复使用相同的名称。在promise already under evaluation: recursive default argument reference or earlier problems? 建立一个可能重复的问题以及来自http://adv-r.had.co.nz/Environments.html#function-envs 的一些解释:

为什么不在每个函数中使用单独的名称,如下所示:

foo1 <- function(x, wd. = wd, reps){
  filename = paste0("Event", x)
  myString <- foo2(file = filename, wd.. = wd.)
  rep(myString, reps)
}

foo2 <- function(file, wd.. = wd){
  filePath <- file.path(wd.., file)
  foo3(Path = filePath)
}

foo3 <- function(Path, wd... = wd){
  paste("Absolute File path is:", Path, "Current working Dir is:", wd...)
}

编辑以响应 cmets 讨论。下面的代码可能会更好地实现您的意图并与您的测试调用 foo1(1, wd = "c:/temp", 2)foo2("C:/temp/otherfilename") 一起使用:

foo1 <- function(x, ..., reps){
  filename = paste0("Event", x)
  myString <- foo2(file = filename, wd = wd)
  rep(myString, reps)
}

foo2 <- function(file, ...){
      filePath <- file.path(wd, file)
  foo3(Path = filePath)
}

foo3 <- function(Path, ...){
  paste("Absolute File path is:", Path, "Current working Dir is:", wd)
}

【讨论】:

  • 嗯,是的,我已经阅读了那个问题(我应该提到它),但试图避免以不同的方式命名它们,因为如果你不必记住哪个单独调用它们会更快使用。这就是我最终采用的解决方法,我只是认为可能有一种更聪明/更普遍接受的方法,因为这似乎是一个常见问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 2013-09-07
相关资源
最近更新 更多