Take this object as an example:
expr <- substitute(mean(exp(sqrt(.)), .))
It is a nested list. I want to find every element that matches quote(.)
.
For example, magrittr
's solution matches only the first level of the call:
dots <- c(FALSE, vapply(expr[-1], identical, quote(.),
FUN.VALUE = logical(1)))
dots
[1] FALSE FALSE TRUE
But I wanted to find every "." in an arbitrary nested list. In this particular case this would be these two dots:
expr[[3]]
expr[[2]][[2]][[2]]
And then these dots should be replaced:
expr[[3]] <- as.name("replacement")
expr[[2]][[2]][[2]] <- as.name("replacement")
expr
# mean(exp(sqrt(replacement)), replacement)
How would you do this?