From hadley at rice.edu  Sun May  8 17:41:35 2011
From: hadley at rice.edu (Hadley Wickham)
Date: Sun, 8 May 2011 10:41:35 -0500
Subject: [Rd] Warning when drop is missing
Message-ID: <BANLkTi=rUDyb6z0kbO_dt0rYFGbfNwwsZQ@mail.gmail.com>

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/



[Rd] Warning when drop is missing
Hadley Wickham