Skip to content

Changing workspace from within an R session

4 messages · gerald.jean@dgag.ca, Frank E Harrell Jr, Brian Ripley +1 more

#
Hello R-users,

is it possible to navigate from one workspace to the other from within an R
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 I
used all the time to navigate between valid Splus directories.  I find this
particularly usefull when I develop new functions.  Most projects I work on
are large to extremely large, developing a function that takes a data.frame
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 happy
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 already
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?
[1] ".GlobalEnv"
 [2] "file:/home/jeg002/splus/GlmExamples/.RData"
 [3] "package:methods"
      ...
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
#
On Tue, 20 Jan 2004 10:51:36 -0500
gerald.jean at dgag.ca wrote:

            
....
Gerald,

I am getting in the habit of explicitly using save(..., compress=TRUE) and
load( ) in R.  Sometimes it adds code but I like the level of control and
the selective saving of only the important objects (usually data frames
and regression fit objects for me).  You could easily make a load2
function that would accept a separate path argument and set the path in a
variable.  Do you have reactions to this approach?  -Frank

---
Frank E Harrell Jr   Professor and Chair           School of Medicine
                     Department of Biostatistics   Vanderbilt University
#
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:

            

  
    
#
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.)

Sean
----- 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:

            
R
I
this
on
data.frame
happy
already
http://www.R-project.org/posting-guide.html
--
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
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html