Documentation for `*tmp*` in R?

北慕城南 提交于 2021-02-11 13:42:13

问题


While skimming through the fuctions section of Hadley Wickham's Advanced R last night I came across this example:

It’s often useful to combine replacement and subsetting

x <- c(a = 1, b = 2, c = 3)
names(x)
#> [1] "a" "b" "c"
names(x)[2] <- "two"
names(x)
#> [1] "a"   "two" "c"

This works because the expression names(x)[2] <- "two" is evaluated as if you had written:

`*tmp*` <- names(x)
`*tmp*`[2] <- "two"
names(x) <- `*tmp*`

(Yes, it really does create a local variable named `*tmp*`, which is removed afterwards.)

I have never seen the *tmp* used before and I'm not really sure what it is, how it works, or when to use it. I also can't find any documentation for it. I find the last comment – that it is removed afterward – to be particularly interesting because it seems that the ability to have variables that automatically remove themselves from the environment could be useful (if used appropriately). Also I have only been able to get this to work intermittently, which is somewhat unusual.

Does anyone know more about this magic `*tmp*` variable?

来源:https://stackoverflow.com/questions/28770882/documentation-for-tmp-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!