Skip to content

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

2 messages · Hadley Wickham, William Dunlap

#
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))
)
[1] 6
Warning message:
drop = TRUE

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

Hadley
#
Or you could write wrapper functions for [ and [<- that
add the drop=FALSE argument.  E.g.,
  SS <- function(x, ..., drop=FALSE) x[..., drop=drop] # 'Safe
Subscripting'
  `SS<-` <- function(x, ..., value) { x[...] <- value ; x }
Use them as
  SS(x, 1, i) # instead of x[1, i] 

Use grep (or codetools::walkCode) to make sure you don't
have any other calls to [ in your code.  (The ss<- is
there only so calls to [ on the left side of the assignment
don't attract grep's attention.)

S+ has a subscript2d() that does this and also lets
you use 2 subscripts on vectors (the 2nd must be 1
in that case, but you don't need to use if statements).
subscript2d doesn't even have the drop= argument - if
you want to make a vector from a one column matrix use
as.vector.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com