# Quote, Unquote and Quasiquote

## Master R

Suppose we are asked to write a test bank of 10,000 simple addition questions for first graders. What shall we do?

Base R has a function quote() that allows us to capture the R code we type instead of evaluating them. For example,

See? We’ve just written one test question, and there’re 9,999 more to go. We certainly don’t want to type quote 9,999 times. How shall we generalize it? Instead of using particular numbers like 1 and 3, we can use variables x and y.

This way, each time we plug in a number for x and y, we’d have a new question.

Wait, quote() still returns x + y! It doesn’t give what we expected. We want 2 + 4. This is because quote() only echos. We need another function to turn off the quotation that quote() turns on, i.e., to “unquote.” The bquote() function in base R can do this. For example,

Alternatively, the funtion rlang::expr() in the rlang package can also achieve this.

Remark: rlang::expr() and base::bquote() are almost equivalent except the latter has a second argument that allows you to pass in an environment where to look up the values of the symbols used in the expression (1st argument). I think this is a general design pattern of rlang and tidyeval, that it separates environments from what a function does as much as possible to make it more consistent and neater.

Finally, we can use a loop to generate 10,000 questions and answers, while randomly choosing x and y values.