[R] Changing workspace from within an R session

Sean Davis sdavis2 at mail.nih.gov
Tue Jan 20 19:22:30 CET 2004

You might consider using emacs and ess where you can work on a text file
(code) as a file, loading it in as needed while running more than one
instance of R within emacs.  You can then code and debug your code in one R
session (where you have your little objects) and jump over to your big
R-session when you want, loading your code there (but it remains a text file
in emacs), try it, go back to code, reload in small R-session, etc.  The
process is fairly straightforward.  (This assumes that your "data" objects
do not need to be communicated between sessions, only code.)

----- Original Message -----
From: "Prof Brian Ripley" <ripley at stats.ox.ac.uk>
To: <gerald.jean at dgag.ca>
Cc: <r-help at stat.math.ethz.ch>
Sent: Tuesday, January 20, 2004 12:12 PM
Subject: Re: [R] Changing workspace from within an R session

Attaching at position 1 does not work in R, as you have found.  That is in
?attach, and in the FAQ.

As the FAQ says, using save()d objects (read-only) can do quite a lot of
this.  You could also save the workspace, clear it, and load another saved
image ... but it is probably easier to restart R.

On Tue, 20 Jan 2004 gerald.jean at dgag.ca wrote:

> Hello R-users,
> is it possible to navigate from one workspace to the other from within an
> session or does one has to close R and restart it from the directory where
> resides the desired workspace?
> For example from Splus I have this little function, see at the end, which
> used all the time to navigate between valid Splus directories.  I find
> particularly usefull when I develop new functions.  Most projects I work
> are large to extremely large, developing a function that takes a
> as input and manipulates it is very time consuming if the input data.frame
> is huge hence I have a valid Splus directory holding a few small
> data.frames, I move to it while developing the function and when I am
> with the function I move this function to my personnal library, always
> attached at position 2, then move back to the the current project's valid
> Splus directory.
> I tried a similar approach in R, from the command line --not using a
> function yet, and I was a bit surprised by the result.  The objects
> in the workspace, as called in R, stayed there and the objects from the
> workspace I wanted to attach were added to the current workspace and the
> workspace I was hoping to attach at pos 1 was attach at pos 2 with
> seemingly nothing in it?
> > attach("/home/jeg002/splus/GlmExamples/.RData", pos = 1)
> > search()
>  [1] ".GlobalEnv"
>  [2] "file:/home/jeg002/splus/GlmExamples/.RData"
>  [3] "package:methods"
>       ...
> > objects(pos = 2)
> character(0)
> The little function, mentioned above, and used in Splus.
> "chdir" <-
> function(datadir,  default.path = '/actuaria/jeg002/')
> {
> # Author  : Gerald Jean
> # Date    : May 1999
> # Purpose : "newdir" will be attached at position 1, and the S directory,
> #           currently at position 1 will be detached.
> # Arguments:
> #  datadir      : the directory to attach.
> #  default.path : the drive on which resides the directory to attach.
> #------------------------------------------------------------------------
> data.dir.to.detach <- search()[1]
> to.attach <- paste(default.path, datadir, "/.Data", sep = "")
> attach(to.attach, pos = 1)
> detach(what = data.dir.to.detach)
> search()
> }
> Thanks for your insights,
> Gérald Jean
> Analyste-conseil (statistiques), Actuariat
> télephone            : (418) 835-4900 poste (7639)
> télecopieur          : (418) 835-6657
> courrier électronique: gerald.jean at spgdag.ca
> "In God we trust all others must bring data"  W. Edwards Deming
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list