我如何使用替换函数创建自定义分配?(How can I create a custom assignment using a replacement function?)

我定义了一个叫做once的函数,如下所示:

once <- function(x, value) { xname <- deparse(substitute(x)) if(!exists(xname)) { assign(xname, value, env=parent.frame()) } invisible() }

这个想法是,评估的value是非常耗时的,我只想在第一次运行脚本时将它分配给x 。

> z Error: object 'z' not found > once(z, 3) > z [1] 3

我真的很喜欢这个用法是once(x) <- value而不是once(x, value) ,但是如果我once<-写入一个函数once<-它变得不安定该变量不存在:

> once(z) <- 3 Error in once(z) <- 3 : object 'z' not found

有没有人有办法解决这个问题?

ps:有没有一个名字来描述函数,如once<-或一般f<- ?

I have 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 is time-consuming to evaluate, and I only want to assign it to x the first time I run a script.

> z Error: object 'z' not found > once(z, 3) > z [1] 3

I'd really like the usage to be once(x) <- value rather than once(x, value), but if I write a function once<- it gets upset that the variable doesn't 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 to describe functions like once<- or in general f<-?

最满意答案

根据R语言参考中的3.4.4项,像名字替换这样的评估是这样评估的:

`*tmp*` <- x x <- "names<-"(`*tmp*`, value=c("a","b")) rm(`*tmp*`)

这对您的需求来说是个坏消息,因为分配在第一行会失败(因为找不到x),即使它能起作用,您的deparse(substitute)调用也不会根据您的要求进行评估。

对不起,让你失望

As per item 3.4.4 in the R Language Reference, something like a names replacement is evaluated like this:

`*tmp*` <- x x <- "names<-"(`*tmp*`, value=c("a","b")) rm(`*tmp*`)

This is bad news for your requirement, because the assignment will fail on the first line (as x is not found), and even if it would work, your deparse(substitute) call will never evaluate to what you want it to.

Sorry to disappoint you

我如何使用替换函数创建自定义分配?(How can I create a custom assignment using a replacement function?)

我定义了一个叫做once的函数,如下所示:

once <- function(x, value) { xname <- deparse(substitute(x)) if(!exists(xname)) { assign(xname, value, env=parent.frame()) } invisible() }

这个想法是,评估的value是非常耗时的,我只想在第一次运行脚本时将它分配给x 。

> z Error: object 'z' not found > once(z, 3) > z [1] 3

我真的很喜欢这个用法是once(x) <- value而不是once(x, value) ,但是如果我once<-写入一个函数once<-它变得不安定该变量不存在:

> once(z) <- 3 Error in once(z) <- 3 : object 'z' not found

有没有人有办法解决这个问题?

ps:有没有一个名字来描述函数,如once<-或一般f<- ?

I have 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 is time-consuming to evaluate, and I only want to assign it to x the first time I run a script.

> z Error: object 'z' not found > once(z, 3) > z [1] 3

I'd really like the usage to be once(x) <- value rather than once(x, value), but if I write a function once<- it gets upset that the variable doesn't 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 to describe functions like once<- or in general f<-?

最满意答案

根据R语言参考中的3.4.4项,像名字替换这样的评估是这样评估的:

`*tmp*` <- x x <- "names<-"(`*tmp*`, value=c("a","b")) rm(`*tmp*`)

这对您的需求来说是个坏消息,因为分配在第一行会失败(因为找不到x),即使它能起作用,您的deparse(substitute)调用也不会根据您的要求进行评估。

对不起,让你失望

As per item 3.4.4 in the R Language Reference, something like a names replacement is evaluated like this:

`*tmp*` <- x x <- "names<-"(`*tmp*`, value=c("a","b")) rm(`*tmp*`)

This is bad news for your requirement, because the assignment will fail on the first line (as x is not found), and even if it would work, your deparse(substitute) call will never evaluate to what you want it to.

Sorry to disappoint you