On 8/08/2008, at 8:39 AM, carol white wrote:

Hi,
How to save an R object for example a matrix or vector and not all objects created in a session (which is usually performed by save.image or q("yes"))?

Two approaches:

(1) Clean up your workspace so that it contains only the objects you really want, before doing q("yes"). Use rm(whatever you don't need). This is probably a Good Idea
anyhow.  Focuses the mind wondrously.

I've written myself a little interactive clean-up function:

        clean <- function ()
        {
                lll <- ls(pos = 1)
                for (xxx in lll) {
                pmpt <- paste("remove ", xxx, "? ", sep = "")
                ans <- readline(pmpt)
                if (ans == "y") remove(list = xxx, pos = 1)
        }
        invisible()
}

Then just type clean() and answer ``y'' for the objects that you want to get rid of.

(2) Save the object that you want to keep in a separate file.  E.g.:

        save(X,file="melvin")

Then, when you want to use the object X, in a new R session, you have to
either (a) load("melvin") or (b) attach("melvin"). Read the help on load() and attach() to see the difference in effect if you are unfamiliar with these
functions.

You could also, instead of using save(), use dput:

        dput(X,"melvin")

Then you want ``X'' in a new R session you do

        X <- dget("melvin")

Note that the *name* ``X'' is not maintained by dput() --- only the ``contents'' of X. So unlike the usage of load() or attach() (applied to the results of save()) you need to *assign* the output of dget() to an object. Which needn't necessarily
be called ``X''.  You could just as easily do

        clyde <- dget("melvin")

Then the ``contents'' of clyde will be the ``contents'' of what you used to call X.
Extra flexibility, more chance to confuse yourself! :-)

        cheers,

                Rolf

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to