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/

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to