[R] Tip: Working directory in titlebar
Michael Prager
Mike.Prager at noaa.gov
Mon Sep 26 01:12:54 CEST 2005
I hope others may find this useful. I use R in many different
directories. By adding the following line to the .Rprofile file:
utils:::setWindowTitle(paste("-",getwd()))
the titlebar of each R instance shows the directory from which it was
started. I have tested this with rterm.exe and rgui.exe under Windows
and with RECENT versions of R only.
-----
For changing directories during a session, I use the following function,
a slight modification of Andy Liaw's cd() function that also changes the
titlebar:
cd <- function(dir = tclvalue(tkchooseDirectory()), saveOld=NA,
loadNew=TRUE) {
# From Andy Liaw ... additions by MHP
# Change the working directory during an R session
#
# First check that the tcl-tk package is available:
stopifnot(require(tcltk))
# Print any error message from trying to load:
if (.Platform$OS.type=="windows") flush.console() ## MHP
# Warn user if saving existing workspace was not chosen: (MHP)
if (is.na(saveOld)) {
tmp = tclvalue(tkmessageBox(message="Save current workspace image?",
type="yesnocancel",title="Changing directories",icon="question"))
saveOld = switch(tmp, "yes"=TRUE, "no"=FALSE,
"cancel"=return(invisible(NULL)))
}
# Save old workspace:
if (saveOld) save.image(compress=TRUE)
# Change directory:
# Note: the need to evaluate "dir" on the next line causes the
"tclvalue" call
# in the function definition (first line) to take place now:
if (dir=="") return(invisible(NULL)) # Fail gracefully if "CANCEL"
chosen from TK dialog (MHP)
setwd(dir)
# Remove all the data from the old working directory:
rm(list=ls(all=TRUE, envir=.GlobalEnv), envir=.GlobalEnv)
# Change the window title:
setWindowTitle(paste("-",dir))
# Load the new data:
if (loadNew) {
if (file.exists(".RData")) {
loaded <- load(".RData", envir=.GlobalEnv)
return(invisible(loaded))
}
else {
cat("Warning: .RData not found in new directory\n")
return(invisible(FALSE))
}
}
}
-----
Although both these bits of code have been used, they may have errors.
Comments and corrections welcome.
--
Michael H. Prager, Ph.D.
Population Dynamics Team
NOAA Center for Coastal Habitat and Fisheries Research
NMFS Southeast Fisheries Science Center
Beaufort, North Carolina 28516 USA
http://shrimp.ccfhrb.noaa.gov/~mprager/
More information about the R-help
mailing list