[R] Multiple copies of attached packages

Fernando Saldanha fsaldan1 at gmail.com
Thu Apr 14 15:24:43 CEST 2005


I have noticed that after I ran a batch script multiple times I get
multiple copies of a package's name when I call search(). Is this a
problem?

> search()
 [1] ".GlobalEnv"        "DF"          "DF"         
[4] "DF"          "DF"          "DF"

multiple copies here ...
 
[13] "DF"          "DF"          "DF"         

other packages here ...

[28] "package:quadprog"  "package:car"       "package:methods"  
[31] "package:stats"     "package:graphics"  "package:grDevices"
[34] "package:utils"     "package:datasets"  "Autoloads"        
[37] "package:base" 

The following strange (to me) behavior that may be related. Suppose I
have a variable x that is in the global environment, and also there is
an 'x' in a dataframe called DF. Then I remove the variable x from the
Global Environment, with

remove('x', pos = 1)
At this point if I call remove again in the same way I get an error:
the variable x does not exist anymore. However, at this point I also
can check that DF$x exists. So far so good.

Further down in my code I have an assignment of the type

x[i] <- something                 (*)

which works fine, except that then if I look at x[i] and DF$x[i] they
are different. So it looks like x was recreated in the Global
Environment, which I actually can check by typing

.GlobalEnv$x.

On the other hand, if I put in my code something like

newvar[i] <- something

where newvar was never defined, then I get an error. I was hoping that
the statement (*)
above would assign to the variable DF$x. But it looks like (although
that is probably not the correct explanation) that the interpreter
somehow "remembers" that once there was a variable x in the global
environment and accepts the assignment to x[i], recreating that
variable.

Any insights on this?

Many thanks,

Fernando




More information about the R-help mailing list