I am trying to write a wrapper around `lm`

that allows the user to specify the optional keywords to `lm`

within the formula. For example, to estimate a linear model of `y`

on `x`

with weights `w`

using the dataset `d`

, I would like for the user to be able to write `newlm(y ~ x | w, d)`

.

As you might guess, the real problem I am working on is more complicated than this, but I am actually stuck on the step of transforming `newlm(y ~ x | w, d)`

into a call to `lm(y ~ x, data = d, weights = w)`

.

While I think I understand how to extract the `w`

part from the formula above, using the `Formula`

package, I can’t figure out how to tell `lm`

to use it. My best guess looks like this:

```
newlm <- function(f, d) {
f <- Formula(f)
f1 <- formula(f, lhs = 1, rhs = 1)
f2 <- formula(f, lhs = 0, rhs = 2)
return(lm(f1, data = d, weights = f2)
}
```

I’m sure that `weights = f2`

isn’t right, but I can’t quite figure out what is. `quo`

, `enquo`

, `substitute`

and a bunch of other things in http://dplyr.tidyverse.org/articles/programming.html I’ve tried don’t quite work. My guess is that this is somehow possible, but I can’t figure out exactly how.

Note: the examples above don’t technically have any `tidy`

in them, but my real problem involves using the variable `w`

inside a bunch of `dplyr`

expressions, so I have already successfully referred to `w`

with a `!!w`

step in those expressions.

Thanks in advance for any suggestions.