[R] Is there a way to specify drop=FALSE as the global default?

hadley wickham h.wickham at gmail.com
Thu Nov 12 00:02:48 CET 2009


> See the above example. Is there a way to make 'drop=FALSE' as global
> default, so that when I say 'tmp[,1]', R will treat it as
> 'tmp[,1,drop=FALSE]'?

The following code won't change the defaults, but it would at least
let you know when you're making the mistake:

trace_all <- function(fs, tracer) {
  lapply(fs, trace, exit = tracer, print=FALSE)
  invisible()
}

functions_with_arg <- function(arg, pos) {
  fs <- ls(pos=pos)
  present <- unlist(lapply(fs, function(x)
    is.function(get(x)) &&  !is.null(formals(x)[[arg]])))

  fs[present]
}

trace_all(
  functions_with_arg("drop", "package:base"),
  quote(if (drop) warning("drop = TRUE", call. = F))
)

> mtcars[1, 2]
[1] 6
Warning message:
drop = TRUE

Unfortunately it doesn't pick up on the generic [ because it is a primitive.

Hadley


-- 
http://had.co.nz/




More information about the R-help mailing list