You can write your code more defensively. There are a few ways to do that:
1. Right after the attach(object), do on.exit(detach(object)).
2. If you know the function will be call repeatedly, it might be a good idea
to check whether the object has been attached. This way you only
On Tue, 2004-02-24 at 09:32, li dongfeng wrote:
Hi there,
I have just found that the ``attach'' function
can get you into trouble when called many times.
For example, you have a simulation routine called ``f()'',
in which you used ``attach'' and no corresponding ``detach''.
Then you call
An alternative to attach is with:
x - with( theta, c(one,two,three) )
---
Date: Tue, 24 Feb 2004 23:32:0 +0800
From: li dongfeng [EMAIL PROTECTED]
To: [EMAIL PROTECTED] [EMAIL PROTECTED]
Subject: [R] be careful: using attach in R functions
Hi there,
I have just found that the
li dongfeng wrote:
Hi there,
I have just found that the ``attach'' function
can get you into trouble when called many times.
[..]
Below is a demonstration of this performance loss,
you will see a linear growth in CPU time usage.
Adding a ``detach()'' call at the end of ``f''
will get
li dongfeng wrote:
Hi there,
I have just found that the ``attach'' function
can get you into trouble when called many times.
For example, you have a simulation routine called ``f()'',
in which you used ``attach'' and no corresponding ``detach''.
Well, attach() may be useful for
Using attach() in this context may not be wise. I tend to only use
attach() when working interactively. It might be better to use with()
in this situation, such as
f - function() {
theta - list(one=2.0, two=0.3, three=0.4)
x - with(theta, c(one, two, three))
sample(x,