R: quote vs expr

quote: returns it’s argument (an expression) as is; variables would be filled in later

substitute: the same, except, variables from the environment are referenced

expr: superficially, the documentation makes it sounds the same as quote. However, a major difference – you can build larger arguments. To prevent evaluation of calls, put in !! operator:

> eval(quote(toupper(quote(letters))))
[1] "LETTERS"

> eval(quote(toupper(!!quote(letters))))
Error in !quote(letters) : invalid argument type

> eval(expr(toupper(!!quote(letters))))
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"

Leave a Reply

Your email address will not be published. Required fields are marked *