Normal subassignment in R is effectively a macro, one which turns a
statement likenames(x)[1] <- "head"
into something like
x <- `names<-`(x, `[<-`(names(x), "head", 1))
However even this explanation is misleading, because the value
returned from a subassignment is the value applied, not the value
assigned. Consider if you wanted to call a function with a
modification of an existing value:
do_something(names(x)[1] <- "head")
Aside from changing the value of x
this actually doesn't
pass the value to do_something
, but rather performs the
equivalent of do_something("head")
.
In this situation, using put
, one can write:
do_something(put(x, names[1], "head"))
codeput and friends are particularly useful in conjunction with
chain
.
x %<~% alter(names[5],="" toupper)<="" code=""> is equivalent to:~%>
names(x)[5] <- toupper(names(x)[5])
x <- inject(1:10, names, letters[.], toupper)
is equivalent to:
x <- 1:10; names(x) <- toupper(letters[x])