问题
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