To make teaching `R`

quasi-quotation easier it would be nice if `R`

string-interpolation and quasi-quotation both used the same notation. They are related concepts. So some commonality of notation would actually be clarifying, and help teach the concepts. We will define both of the above terms, and demonstrate the relation between the two concepts.

## String-interpolation

String-interpolation is the name for substituting value into a string. For example:

```
library("wrapr")
variable <- as.name("angle")
sinterp(
'variable name is .(variable)'
)
```

`## [1] "variable name is angle"`

Notice the "`.(variable)`

" portion was replaced with the actual variable name "`angle`

". For string interpolation we are intentionally using the "`.()`

" notation that Thomas Lumley’s picked in 2003 when he introduced quasi-quotation into `R`

(a different concept than string-interpolation, but the topic of our next section).

String interpolation is a common need, and there are many `R`

packages that supply variations of such functionality:

`base::sprintf`

`R.utils::gstring()`

`rprintf::rprintf()`

`stringr::str_interp()`

`glue::glue()`

`wrapr::sinterp()`

(requires version 1.8.3, or newer).

## Quasi-quotation

A related idea is "quasi-quotation" which substitutes a value into a general expression. For example:

```
angle = 1:10
variable <- as.name("angle")
evalb(
plot(x = .(variable),
y = sin(.(variable)))
)
```

Notice how in the above plot the actual variable name "`angle`

" was substituted into the `graphics::plot()`

arguments, allowing this name to appear on the axis labels.

`evalb()`

is a very simple function built on top of `base::bquote()`

:

`print(evalb)`

```
## function(..., where = parent.frame()) {
## force(where)
## exprq <- bquote(..., where = where)
## eval(exprq,
## envir = where,
## enclos = where)
## }
## <bytecode: 0x7fa0181b4470>
## <environment: namespace:wrapr>
```

All `evalb()`

does is: call `bquote()`

as intended. A way to teach this is to just call `bqoute()`

alone.

```
bquote(
plot(x = .(variable),
y = sin(.(variable)))
)
```

`## plot(x = angle, y = sin(angle))`

And we see the un-executed code with the substitutions performed.

There are many `R`

quasi-quotation systems including:

`base::bquote()`

`gtools::strmacro()`

`lazyeval`

`wrapr::let()`

`rlang::as_quosure()`

`nseval`

If you don’t want to wrap your `plot()`

call in `evalb()`

you can instead pre-adapt the function. Below we create a new function `plotb()`

that is intended as shorthand for `eval(bquote(plot(...)))`

.

```
plotb <- bquote_function(graphics::plot)
plotb(x = .(variable),
y = sin(.(variable)))
```

## Conclusion

When string-inerpolation and quasi-quotation use the same notation we can teach them quickly as simple related concepts.

Categories: Opinion Programming Tutorials

### jmount

Data Scientist and trainer at Win Vector LLC. One of the authors of Practical Data Science with R.

Great idea! Useful not only for teachers but for everyday end-users (like me) too!

LikeLike

I installed wrapr 1.82 and the sinterp function is not included in the package. Also, it is not listed in the package manual https://cran.r-project.org/web/packages/wrapr/wrapr.pdf

LikeLike

Yes, sinterp() requires wrapr 1.8.3 which should be released to CRAN in early February. If you want to try it now the dev-version can be installed with

`devtools::install_github("WinVector/wrapr")`

.LikeLike