[R] Using data=x or subset=y in user-defined functions

Manuel Morales Manuel.A.Morales at williams.edu
Wed Jun 7 22:01:36 CEST 2006


Dear list members,

In some of my functions, I attach the data internally to allow subset
commands or to specify a data frame. This works well except for cases
where there is a "masking" conflict (which returns a warning). I see
some alternative listed in ?attach, but I'm not sure which of them do
what I'd like. Any suggestions?

Below is how I've been setting up my functions:

eg.function <- function(x, data=NULL, subset=NULL, ...) {

# Set up environment
on.exit(detach(data))
attach(data)
if(!is.null(subset)) {
    data<-subset(data,subset)
detach(data)
attach(data)
}
subset = NULL
 
# Function body here
output <- x   
return(output)
}

Thanks!

Manuel



More information about the R-help mailing list