[Rd] Advice on package design for handling of dots in a formula

S Ellison S.Ellison at LGCGroup.com
Wed Oct 15 17:50:10 CEST 2014

> This seems to be a common approach in other packages. However, one of my
> testers noted that if he put formula=y~. then w, ID, and site showed up in the
> model where they weren't supposed to be. 

This is the documented behaviour for '.' in a formula - it means 'everything else in the data object'

Without changing your current code, though, your user could have said something like

if they wanted to specify 'everything _except_ the subtracted terms', so it's not as bad as having no shortcuts at all.

If you want to do the work for them, one (probably crude) way of doing it could use drop.terms() in combination with some work with the term labels:

#A function that drops the terms in two later arguments from the terms in the first and returns the resulting trimmed terms object.
f <- function(form, dropthis, dropthattoo, data) {
	everything <- attr(terms(form, data=data), "term.labels") #needs data to expand '.'
	drops <- c(attr(terms(dropthis, data=data), "term.labels"), 
			attr(terms(dropthattoo, data=data), "term.labels")) #could probably do without 'data'
	excludes <-which(everything %in% drops)
	terms(form, data=data)[-excludes]

d <- data.frame(a=1:10, b=10:1, g=gl(5,2), g2=gl(2,5), y=rnorm(10))

f(y~., ~g, ~b, data=d)
	#This returns a terms object, but there's a formula in that if you want it....

formula(f(y~., ~g, ~b, data=d))

 You'll need to be careful about evaluating that though; don't forget to give any relevant model or model matrix functions the environment (data frame) to go with it or you'll get nonsense.


This email and any attachments are confidential. Any use...{{dropped:8}}

More information about the R-devel mailing list