[R] Summary: Changing workspace from within an R session

gerald.jean@dgag.ca gerald.jean at dgag.ca
Wed Jan 21 20:38:52 CET 2004

Thanks to Frank Harrell, Brian Ripley, Andy Liaw, Jeff Laake and Sean Davis
for their usefull comments regarding "navigation" between valid R

Jeff recommanded the Mark Bravington package that can be found at:
(ftp://ftp.marine.csiro.au/software/bravington/).  I looked at the package
and found that it can do a lot more than what I asked for.  At this time I
was not prepared to go to the extent of installing the whole package,
reading the manuals, etc. only to accomplish my little task, but the
package seems to have a lot to offer.

I ended up revamping my original Splus function, based on the comments of
responders to my original post.  For those interested here is the little
function, works like a charm.  Note that "options()$Current.Dir" gets setup
at startup, note also that the name of all my temp. objects start by "ttt".

"chdir" <-
function(datadir,  default.path = '/actuaria/jeg002/')
# Author  : Gérald Jean, based on ideas from different people on the
R-users mailing  #           list.
# Date    : January 21, 2004
# Purpose : will delete all objects starting with "ttt*" from directory
#           currently at position 1 in the search path, will save the
#           ".GlobalEnv" where it belongs, will cleanup workspace and
#           load "datadir" into the workspace.
# Arguments:
#  datadir      : the directory to attach.
#  default.path : the drive on which resides the directory to attach.

### Remove temporary objects from .GlobalEnv, they all start by "ttt".

  remove(list = objects(pattern = "^ttt", pos = 1), pos = 1)

### Save current .GlobalEnv to .RData where it comes from, which is given
### by options()$Current.Dir

  save(list = objects(pos = 1),
       file = paste(options()$Current.Dir, '/.RData', sep = ''))

###  Remove everything from .GlobalEnv but ".whatever" objects.

  remove(list = objects(pos = 1), pos = 1)

### If it exists, load the .RData from "datadir" into workspace.

  path <- paste(default.path, datadir, sep = "")
  to.load <- paste(path, "/.RData", sep = '')
  if (file.exists(to.load)){
    load(to.load, envir = .GlobalEnv)
    options(Current.Dir = path)
    cat(paste("\n\tNothing loaded ", to.load, " doesn't exist!!!", sep
= ''),
        sep = '\n')

Thanks again to all respondents,

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

More information about the R-help mailing list