Is there a way to specify drop=FALSE as the global default?
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of hadley wickham Sent: Wednesday, November 11, 2009 3:03 PM To: Peng Yu Cc: r-help at stat.math.ethz.ch Subject: Re: [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:
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
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/
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.