[Rd] issues with environment handling in model.frame()

Antoine Fabri @nto|ne@|@br| @end|ng |rom gm@||@com
Sat May 2 14:30:29 CEST 2020


Dear all,

model.frame behaves in a way I don't expect when both its formula and
subset argument are passed through a function call.

This works as expected:

model.frame(~wool, warpbreaks, breaks < 15)
#>    wool
#> 14    A
#> 23    A
#> 29    B
#> 50    B
fun1 <- function(y) model.frame(~wool, warpbreaks, y)
fun1(with(warpbreaks, breaks < 15))
#>    wool
#> 14    A
#> 23    A
#> 29    B
#> 50    B

but this doesn't:

fun2 <- function(x, y) model.frame(x, warpbreaks, y)
fun2(~wool, with(warpbreaks, breaks < 15))
#> Error in eval(substitute(subset), data, env): object 'y' not found

model.frame is used by xtabs() and aggregate() so the following won't work
either:
fun3 <- function(x, y) xtabs(x, warpbreaks, y)
fun3(~wool, with(warpbreaks, breaks < 15))
#> Error in eval(substitute(subset), data, env): object 'y' not found

fun4 <- function(x, y) aggregate(x, warpbreaks, length, subset = y)
fun4(breaks ~ wool, with(warpbreaks, breaks < 15))
#> Error in eval(substitute(subset), data, env): object 'y' not found

The subset argument uses NSE, though no help file among aggregate, xtabs or
model.frame suggests it as far as I can see, and I assume the expectation
that NSE will be used led to overlook the problematic case above.

The SO question prompted this bug report :
https://stackoverflow.com/questions/61145404

Best regards,

Antoine

	[[alternative HTML version deleted]]



More information about the R-devel mailing list