s-news
[Top] [All Lists]

Re: Error: Too many databases in use?

To: s-news@lists.biostat.wustl.edu
Subject: Re: Error: Too many databases in use?
From: Sarah Henderson <sarah.henderson@ubc.ca>
Date: Sun, 29 Apr 2007 19:58:44 -0700
In-reply-to: <A23B682083FD8248ADFE1317C82CBD6702734347@exchange.branznt.org.nz>
References: <7.0.1.0.2.20070429002756.02508bc8@ubc.ca> <A23B682083FD8248ADFE1317C82CBD6702734347@exchange.branznt.org.nz>

Many thanks to Michael for such a detailed and helpful reply. I had the wrong impression of get() and I now know that it makes a temporary attachment.

In the end I went with suggestion #1, which worked a charm. I did try to implement suggestions #2 and #3, but DATA.FRAME[NAME1] produces a new data frame instead of a vector, and this affected my downstream operations. I couldn't seem to coerce it into vector of mode double. Should this have worked?

VECTOR1 <- as.vector(DATA.FRAME[NAME1], mode="double")?

The language reference wasn't very helpful in this case.

Anyhow, the script is now working and that's a victory in my book. Good programming practice is a luxury for a hack like me!

Thanks again, Michael.

Sarah

I think what is happening is that when you use get() on an object it
temporarily attaches that object as a database. That connection only
gets broken when either the function call ends (in this case lapply), or
the S+ session ends. Basically with the way you have coded it you are
attaching DATA.FRAME every time you call get and not detaching it,
leading to ballooning databases.

You could try calling synchronize in PAIRWISE, and that might sort
things out. You could also try to detach DATA.FRAME before exiting
PAIRWISE.

Several ways around it:

1) Attach DATA.FRAME before your call lapply then access it in the
function call as in:

attach(DATA.FRAME)
lapply(.....)
...

NAME1 <- paste("ONE", ID, sep="")
VECTOR1 <- get(NAME1)

Or attach DATA.FRAME explicitly in PAIRWISE, and detach it explicitly
before exiting PAIRWISE. This should stop the databases ballooning.
PAIRWISE(...)
...
        attach(DATA.FRAME)
        NAME1 <- paste("ONE", ID, sep="")
        VECTOR1 <- get(NAME1)
        NAME2 etc
        detach(DATA.FRAME)

2) You could just access the object directly as in

NAME1 <- paste("ONE", ID, sep="")
VECTOR1 <- DATA.FRAME[NAME1]

3) Pass DATA.FRAME as an argument to PAIRWISE and access it in the
function.

My preference is 3) as that is good programming practice. S+ is quite
happy to pass very large objects into functions.

In my experience attaching data frames is not intended for heavy duty
data processing. It is really intended as a convenience when you are
doing small or interactive analyses so that you can deal directly with a
limited set of variable names as objects, rather than having to
continually refer to the full data frame.

Michael

MICHAEL CAMILLERI BSc, MSc, PhD
BUILDING PHYSICIST
T +64 4 237 1170
DDI +64 4 237 1174
PRIVATE BAG 50908
PORIRUA CITY 5240
WWW.BRANZ.CO.NZ
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news


<Prev in Thread] Current Thread [Next in Thread>