I defined a function called once as follows:
once <- function(x, value) { xname <- deparse(substitute(x)) if(!exists(xname)) { assign(xname, value, env=parent.frame()) } invisible() }
The idea is that value takes a lot of time to evaluate, and I only want to assign it x when the script is first run.
> z Error: object 'z' not found > once(z, 3) > z [1] 3
I would really like the use to be once(x) <- value , and not once(x, value) , but if I write the function once<- , it will be frustrated that the variable does not exist:
> once(z) <- 3 Error in once(z) <- 3 : object 'z' not found
Does anyone have a way around this?
ps: is there a name for describing functions like once<- or even f<- ?
source share