0

I am having a hard time understanding how quoting, unquoting, quasiquotation... works in R. In this case I wanted to fit a linear model. I think ususally you do not need to quote the input to the lm-call.

So I wanted to do something like this:

names(mtcars)

y = "mpg" # response
x = "hp" # predictor

fml = y ~ x

lm(fml, data=mtcars)

But obviously this does not work as literally "y" and "x" are passed int the formula. I tried several things with expr() and eval() but I think I am not on the right way. Reading the chapter on Metaprogramming in advanced R would certainly help a lot, but maybe there is also an "intuitive" solution here.

Lenn
  • 1,283
  • 7
  • 20
  • `y = as.name("mpg"); x = as.name("hp"); fml = eval(bquote(.(y) ~ .(x)))` However, it's usually better to substitute into the `lm` call, e.g., `eval(bquote(lm(.(y) ~ .(x))))`, as otherwise you could run into scoping issues since the wrong environment could be associated with the formula. – Roland Jun 01 '21 at 07:18

2 Answers2

3

For lm you don't need quoting/unquoting. You can use as.formula or reformulate to construct the formula.

lm(reformulate(y, x), data=mtcars)

However, since lm also accepts strings that can be coerced to formula you can use paste which will give the same result.

lm(paste(x, y, sep = '~'), data=mtcars)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

If you wanted to use the rlang style quoting/unquoting, you would use.

y = sym("mpg")
x = sym("hp")

# alternatively
# y = expr(mpg)
# x = expr(hp)


fml = inject(!!y ~ !!x)

lm(fml, data=mtcars)
MrFlick
  • 195,160
  • 17
  • 277
  • 295