What does (substitute ()) do?

I'm not entirely sure I understand what it does substitute

, although I've used code in it before. Today I came across in the shiny::exprToFunction

following lines of code:

function (expr, env = parent.frame(2), quoted = FALSE, caller_offset = 1) 
{
    expr_sub <- eval(substitute(substitute(expr)), 
...
}

      

Can someone explain why nested is used here substitute

? An easy-to-work example will really help.

+3


source to share


1 answer


Take a look at

a<-function(aa) {
    b(aa)
}

b<-function(bb) {
    z(bb)   
}

z<-function(zz) {
    print(substitute(zz))
    print(substitute(substitute(zz)))
    print(eval(substitute(substitute(zz)), parent.frame()))
}

q<-5
a(q)
# bb
# substitute(bb)
# aa

      



The first / inner replacement captures the name / character passed to the called function. The second / outer substitute()

just wraps the command substitute()

around the detected name / symbol. It substitute()

is then evaluated in the parent environment in which it appeared.

The use method substitute

to capture variable names only works when the parameters are still promises; that is, they have not yet been evaluated.

+6


source







All Articles