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"))? Best, Carol [[alternative HTML version deleted]]
See ?save On Thu, Aug 7, 2008 at 5:39 PM, carol white <wht_crl at yahoo.com> 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"))? > > Best, > > Carol > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
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}}
On 11/08/2008, at 9:57 PM, carol white wrote:> Thanks for your replies. > > So there is no way to add one or more objects to the existing > objects stored in .RData?Not really. There is no ``append=TRUE'' argument for save(). There is a workaround, of course. There [almost] always is in R: attach(".RData",warn.conflicts=FALSE) assign("clyde",clyde,pos=2) savepos(2) detach(2) where ``clyde'' is the object you are trying to ``save selectively''. ***At this point*** the file .RData will contain the objects that were in it when you started your R session *and* ``clyde''. However, it could easily get mucked up. In particular, if you were to answer ``y'' to the question ``Save workspace image?'' when you quit, then .RData will get overwritten, and will then contain whatever objects were in your global environment at the time you quit. Hence I would ***STRONGLY ADVISE YOU NOT TO PROCEED IN THIS MANNER***. You are trying to use .RData in a way in which it was not intended to be used, and consequently tying yourself in knots. If you want to save objects selectively, save them in a different file from .Rdata, and then load or append (or dget()) that file, as I suggested in my previous email. Use the facilities of R in the way that they were intended to be used, rather than trying to force your own inappropriate paradigm upon R. You'll find it more efficient to do things R's way rather than trying to force R to do things your way. R gets it right. [Almost] always! cheers, Rolf Turner P. S. ``Almost always'' == ``except on a set of measure/probability zero''. :-) ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}