[R] Some Windows code for GUI-izing workspace loading
Bert Gunter
gunter.berton at gene.com
Thu Jan 4 22:54:53 CET 2007
Folks:
Motivated by the recent thread on setting working directories, below are a
couple of functions for GUI-izing saving and loading files **in Windows
only** that sort of takes care of this automatically. The simple strategy
is just to maintain a file consisting of the filenames of recently saved
workspace (.Rdata, etc.)files. Whenever I save a workspace via the function
mySave() below, the filename is chosen via a standard Windows file browser,
and the filename where the workspace was saved is added to the list if it
isn't already there. The recent() function then reads this file and brings
up a GUI standard Windows list box (via select.list()) of the first k
filenames (default k = 10) to load into the workspace **and** sets the
working directory to that of the first file loaded (several can be brought
in at once).
I offer these functions with some trepidation: they are extremely simple and
unsophisticated, and you definitely use them at your own risk. There is no
checking nor warning for whether object names in one loaded file duplicate
and hence overwrite those in another when more than one is loaded, for
example. Nevertheless, I have found the functions handy, as I use the
"recently used files" options on all my software all the time and wanted to
emulate this for R.
Suggestions for improvement (or better yet, code!) or information about bugs
or other stupidities gratefully appreciated.
Cheers,
Bert Gunter
Genentech Nonclinical Statistics
South San Francisco, CA 94404
#### Code Follows #####
mySave<-
function(recentlistFile=paste("c:/Program
Files/R","recentFiles.txt",sep="/"),
savePlots=FALSE)
{
## DESCRIPTION:
## Use a windows GUI to save current workspace
## ARGUMENTS:
## recentlistFile: a quoted character string giving the full
pathname/filename to
## the file containing the listof recent files.
## This must be the same as the "filename" argument of recent()
## The default saves the file in the global R program directory, which
means it does not
## have to be changed when updating to new versions of R which I store
under
## the global R directory. You may need to change this if you have a
different
## way of doing things.
##
##
## savePlots: logical. Should the .SavedPlots plot history be saved? This
object can
## be quite large and not saving it often makes saving and loading much
faster,
## as well as avoiding memory problems. The default is not to save.
if(!savePlots) if(exists(".SavedPlots",where=1))rm(.SavedPlots,pos=1)
fname<-choose.files(caption='Save
As...',filters=Filters['RData',],multi=FALSE)
if(fname!=""){
save.image(fname)
if(!file.exists(recentlistFile))write(fname,recentlistFile,ncol=1)
else{
nm<-scan(recentlistFile,what="",quiet=TRUE,sep="\n")
## remove duplicate filenames and list in LIFO order
write(unique(c(fname,nm)),recentlistFile,ncol=1)
}
}
else cat('\nWorkspace not saved\n')
}
recent<-
function(filename=paste("c:/Program
Files/R","recentFiles.txt",sep="/"),nshow=10,
setwork=TRUE)
{
## DESCRIPTION:
## GUI-izes workspace loading by bringing up a select box of files
containing
## recently saved workspaces to load into R.
## ARGUMENTS:
## file: character. The full path name to the file containing the
file list,
## which is a text file with the filenames, one per line.
##
##
## nshow: The maximum number of paths to show in the list
##
## setwork: logical. Should the working directory be set to that of
the first file
## loaded?
## find the file containing the filenames if it exists
if(!file.exists(filename))
stop("File containing recent files list cannot be found.")
filelist<-scan(filename,what=character(),quiet=TRUE,sep='\n')
len<-length(filelist)
if(!len)stop("No recent files")
recentFiles<-select.list(filelist[1:min(nshow,len)],multiple=TRUE)
if(!length(recentFiles))stop("No files selected")
i<-0
for(nm in recentFiles){
if(file.exists(nm)){
load(nm,env=.GlobalEnv)
i<-i+1
if(i==1 &&setwork)setwd(dirname(nm))
}
else cat('\nFile',nm,'not found.\n')
}
cat('\n\n',i,paste(' file',ifelse(i==1,'','s'),' loaded\n',sep=""))
}
More information about the R-help
mailing list