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.
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 <- 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 <- 100
myfunc(x=300)
[1] 300
Thanks in advance!
We can use the ls() function to show what variables and functions are defined in the current environment. Moreover, we can use the environment() function to get the current environment. In the above example, we can see that a , b and f are in the R_GlobalEnv environment.
Accessing Global VariablesGlobal Variables can be accessed from anywhere in the code unlike local variables that have a scope restricted to the block of code in which they are created.
ls() function in R Language is used to list the names of all the objects that are present in the working directory.
Check if an Object of the Specified Name is Defined or not in R Programming – exists() Function. exists() function in R Programming Language is used to check if an object with the names specified in the argument of the function is defined or not. It returns TRUE if the object is found.
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.
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
myfunc()
[1] 30
If x
is found it would return it. if 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:
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With