如果对象存在于全局中,则R从函数中获取来自全局环境的对象,否则使用不同的默认值

时间:2022-03-09 23:35:35

Surely this is possible, but I can't seem to find how to do it:

当然这是可能的,但我似乎无法找到如何做到这一点:

I'd like to have a default of a function input, but override the default and get() a variable from the global environment if it exists in the global environment. If it doesn't exist in the global environment, take the default of the function, with any setting in the function being top level and overriding them all.

我想要一个函数输入的默认值,但是如果它存在于全局环境中,则覆盖默认值和get()一个来自全局环境的变量。如果它在全局环境中不存在,请使用该函数的默认值,该函数中的任何设置都是*并覆盖它们。

Ideally it would work like this made-up non-working function:

理想情况下,它会像这个非工作函数一样工作:

###Does not work, desired example

myfunc <- function(x=30){
if(exists.in.global.env(x)){x <- get(x)}
###Top level is tough
if(x.is.defined.as.function.input=TRUE ????){x <- x.defined.as.input}
}else{ x <- 30} 
return(x)
}

So that if I do:

如果我这样做:

myfunc()
[1] 30

But if I create x I want it to override the default x=30 of the function and take the global environment value instead:

但是如果我创建x,我希望它覆盖函数的默认x = 30并取代全局环境值:

x <- 100
myfunc()
[1] 100

But if I have x defined inside the function, I'd like that to be top level, i.e. override everything else even if x is defined globally:

但是如果我在函数内部定义了x,那么我希望它是*的,即使全局定义x,也要覆盖其他所有内容:

x <- 100
myfunc(x=300)
[1] 300

Thanks in advance!

提前致谢!

2 个解决方案

#1


14  

You can modify your function to check if x exists in the .GlobalEnv and get it from there if it does, otherwise return the default value.

您可以修改函数以检查.GlobalEnv中是否存在x,如果存在则从中获取x,否则返回默认值。

myfunc <- function(x = 30) {

  if ("x" %in% ls(envir = .GlobalEnv)) {
    get("x", envir = .GlobalEnv)
  } else {
    x
  }

}

So if "x" %in% ls(envir = .GlobalEnv) is FALSE it would return

因此,如果%ls(envir = .GlobalEnv)中的“x”%为FALSE,则返回

myfunc()
[1] 30

If x is found it would return it. if x <- 100:

如果找到x,它将返回它。如果x < - 100:

myfunc()
[1] 100

Edit after comment

评论后编辑

If you want to make sure to only return x from the global environment if x is not specified as an argument to myfunc, you can use missing(). It returns TRUE if x was not passed and FALSE if it was:

如果要确保仅从全局环境返回x,如果未将x指定为myfunc的参数,则可以使用missing()。如果没有传递x则返回TRUE,如果是,则返回FALSE:

myfunc <- function(x = 30) {

  if ("x" %in% ls(envir = .GlobalEnv) & missing(x)) {
    get("x", envir = .GlobalEnv)
  } else {
    x
  }

} 

So for your example:

所以对你的例子:

x <- 100
myfunc(x=300)
[1] 300

#2


3  

The simplest method would be to set an appropriate default argument:

最简单的方法是设置适当的默认参数:

myfunc <- function(x=get("x", globalenv())
{
    x
}

> x <- 100
> f()
[1] 100
> f(30)
[1] 30
> rm(x)
> f()
Error in get("x", globalenv()) : object 'x' not found

#1


14  

You can modify your function to check if x exists in the .GlobalEnv and get it from there if it does, otherwise return the default value.

您可以修改函数以检查.GlobalEnv中是否存在x,如果存在则从中获取x,否则返回默认值。

myfunc <- function(x = 30) {

  if ("x" %in% ls(envir = .GlobalEnv)) {
    get("x", envir = .GlobalEnv)
  } else {
    x
  }

}

So if "x" %in% ls(envir = .GlobalEnv) is FALSE it would return

因此,如果%ls(envir = .GlobalEnv)中的“x”%为FALSE,则返回

myfunc()
[1] 30

If x is found it would return it. if x <- 100:

如果找到x,它将返回它。如果x < - 100:

myfunc()
[1] 100

Edit after comment

评论后编辑

If you want to make sure to only return x from the global environment if x is not specified as an argument to myfunc, you can use missing(). It returns TRUE if x was not passed and FALSE if it was:

如果要确保仅从全局环境返回x,如果未将x指定为myfunc的参数,则可以使用missing()。如果没有传递x则返回TRUE,如果是,则返回FALSE:

myfunc <- function(x = 30) {

  if ("x" %in% ls(envir = .GlobalEnv) & missing(x)) {
    get("x", envir = .GlobalEnv)
  } else {
    x
  }

} 

So for your example:

所以对你的例子:

x <- 100
myfunc(x=300)
[1] 300

#2


3  

The simplest method would be to set an appropriate default argument:

最简单的方法是设置适当的默认参数:

myfunc <- function(x=get("x", globalenv())
{
    x
}

> x <- 100
> f()
[1] 100
> f(30)
[1] 30
> rm(x)
> f()
Error in get("x", globalenv()) : object 'x' not found