Search notes:

R function: quote

q <- quote( z <- x*y )

q
# z <- x * y
Github repository about-r, path: /functions/quote/basic.R

typeof, class and mode

The type of the value returned by quote is "language", the class and mode is "call":
q <- quote (3 * x);

typeof(q);
#
#  language

class(q);
#
#  call

mode(q);
#
#  call
Github repository about-r, path: /functions/quote/typeof-class-mode.R
These returned strings are the same as those returned by substitue.

Changing an expression

quote can be used to change an expression because «code» is just a plain list.
expr <- quote( 100 - (4 + 10) );

eval(expr);
#
#  86

show_tree <- function(e, indent) {
  for (i in 1:length(e)) {
     if (length(e[[i]]) == 1) {
        cat(paste0(strrep('  ', indent), e[[i]]), "\n")
     }
     else {
       show_tree(e[[i]], indent+1);
     }
  }
}

show_tree(expr, 0)
#
#  - 
#  100 
#    ( 
#      + 
#      4 
#      10 

#
#  With the tree, we can determine the position
#  of the plus in the tree:
#
expr[[3]][[2]][[1]]
#
#  `+`

#
#  Change the plus to a multiplication:
#
expr[[3]][[2]][[1]] <- quote(`*`);

expr
#
#  100 - (4 * 10)

eval(expr);
#
#  60
Github repository about-r, path: /functions/quote/change-expression.R

See also

bquote
Index to (some) R functions

Index