[R] Problem with data.frames

John Fox jfox at mcmaster.ca
Mon Aug 4 15:13:07 CEST 2003


Dear Simon,

A solution that skirts the issue is to introduce a new function -- something like

Attach <- function (what, pos=2, name=deparse(substitute(what))) {
    detach(pos = match(name, search()))
    attach(get(name, envir=.GlobalEnv), pos=pos, name=name)
    }

Then, when the previously attached version of the object is different from the current one, the previous version is detached. I think that this is the behaviour that would usually be desired, though there might be occasions where you want to preserve the previous version.

Regards,
 John

----------------------------------------------------
John Fox
Department of Sociolgy
McMaster University
http://socserv.mcmaster.ca/jfox
----------------------------------------------------

> ------------Original Message-------------
> From: "Simon Fear" <Simon.Fear at synequanon.com>
> To: "Prof Brian Ripley" <ripley at stats.ox.ac.uk>
> Cc: Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk>, r-help at stat.math.ethz.ch, Andreas Eckner <andreas.eckner at soundinvest.net>
> Date: Mon, Aug-4-2003 8:04 AM
> Subject: RE: [R] Problem with data.frames
> 
> What I meant by "like library()" is that this function ensures that a
> library
> (or should I say package?) does not get loaded twice / duplicated in the
> search path. Of course attach() is not the same as library(). Nor is it
> the
> same as with() - I just thought it might be useful if the help pages
> pointed
> to each other. Because I have programmed for years using attach() and
> detach() and only very recently discovered with() (which is nearly
> always
> what I want since I work mostly with scripts).
> 
> > So how can you tell if the data frame is on the search list
> 
> "my.dframe" %in% search() ?
> 
> Does anyone have an example of a case where attaching the same data
> frame
> twice would be useful?
> 
> Thanks Prof R. for (another) reminder to read the code - I'm always
> forgetting to look there. RTFC, Simon!
> 
> SF
> 
> -----Original Message-----
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> Sent: 04 August 2003 12:06
> To: Simon Fear
> Cc: Peter Dalgaard BSA; Andreas Eckner; r-help at stat.math.ethz.ch
> Subject: RE: [R] Problem with data.frames
> 
> 
> On Mon, 4 Aug 2003, Simon Fear wrote:
> 
> > Peter is right, but there is a point here: it would be nice if
> > attach(my.dframe) would do nothing - or at least warn - if my.dframe
> was
> > already in the search list. Like library(). Attaching twice is almost
> 
> It is not like library().  attach attaches a copy of the data frame, and
> it can be altered subsequently. So how can you tell if the data frame is
> on the search list.  I suspect it is quite common to create an object
> called `tmp' and put it on the search list for a while.  library()
> assumes
> that you have not reinstalled a loaded package (and if you have you
> would
> get inconsistent results).
> 
> > bound
> > to be an error?
> 
> Not at all.
> 
> > Of course in many circumstances it might be better to use with() than
> > attach() - if you haven't come across with(), it works like attach()
> > with an
> > inbuilt detach() when the parentheses close.
> > 
> > Aside 1: it would also be nice if ?attach pointed to ?with. Is this
> kind
> > of
> > suggestion best sent to r-help or r-devel?
> 
> I don't see they do the same job, especially not in an interactive 
> session (which is when attach() is most usefully used in this way).
> 
> > Aside 2: is with() efficient, or does it create a copy of the dataset?
> It does not create a copy, as you can see from the code (it uses a 
> three-argument eval).
> 
> -- 
> Brian D. Ripley,                  ripley at 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
>  
> 
> Simon Fear
> Senior Statistician
> Syne qua non Ltd
> Tel: +44 (0) 1379 644449
> Fax: +44 (0) 1379 644445
> email: Simon.Fear at synequanon.com
> web: http://www.synequanon.com
>  
> Number of attachments included with this message: 0
>  
> This message (and any associated files) is confidential and\...{{dropped}}
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
>




More information about the R-help mailing list