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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Thu 08 Jun 2006 - 06:36:50 EST

I suggest you investigate with().

On Wed, 7 Jun 2006, Manuel Morales wrote:

> 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
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

-- 
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Thu Jun 08 06:43:18 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 09 Jun 2006 - 04:10:38 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.