Peng Yu
2009-Oct-20 01:57 UTC
[R] Is there a way to specify drop=FALSE as the global default?
tmp <- matrix(1:2) tmp tmp[,1,drop=FALSE] 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]'?
Peng Yu
2009-Nov-11 17:11 UTC
[R] Is there a way to specify drop=FALSE as the global default?
On Mon, Oct 19, 2009 at 7:57 PM, Peng Yu <pengyu.ut at gmail.com> wrote:> tmp <- matrix(1:2) > tmp > tmp[,1,drop=FALSE] > > > 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]'?Is there a way to set drop=FALSE globally? I can't remember to put drop=FALSE to all the '[]'s. But this indeed causes some weird bugs for me to figure out.
hadley wickham
2009-Nov-11 23:02 UTC
[R] Is there a way to specify drop=FALSE as the global default?
> 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/