[Rd] Warning when drop is missing

Hadley Wickham hadley at rice.edu
Sun May 8 17:41:35 CEST 2011


Hi all,

One common bug I find in my code is that I've forgotten to specify
drop = F when subsetting a data frame, and then my code breaks when
the result is unexpectedly a vector. Does anyone have an effective
technique for preventing this from happening?  I have an approach (see
below) that works for the similar case of not explicitly specifying
na.rm behaviour, but since the subsetting functions are primitive this
approach doesn't work.

Thanks!

Hadley

  objs <- c(ls("package:base", "package:stats"))
  has_missing_arg <- function(name) {
    x <- get(name)
    if (!is.function(x)) return(FALSE)

    args <- names(formals(x))
    "na.rm" %in% args
  }
  f_miss <- Filter(has_missing_arg, objs)

  trace_all <- function(fs, tracer) {
    sapply(fs, trace, tracer = tracer, print = FALSE)
    return()
  }

  trace_all(f_miss, quote(if(missing(na.rm)) stop("na.rm not set")))


-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-devel mailing list