[R] R environment

Henrik Bengtsson hb at maths.lth.se
Thu Sep 1 08:45:17 CEST 2005

Unfortunately Duncan's suggestion to restart is probably the only way to 
go here.  I've done similar thing myself too.  What I've learned was 
that it is clever to include a so called hot-patch mechanism in your 
code, which will load R source code found in a certain directory, say, 
"hot/", once in a while (easy if you have do interations) and at the end 
of the batch code. Here is the idea (typed out of my head):

patchCode <- function(path="hot", pattern="[.]R$", removeAfter=FALSE, ...) {
   files <- list.files(pattern=pattern, path=path, full.names=TRUE);
   for (file in files) {
       # You don't want you patch code to kill you batch job,
       # if you for instance have a typo.
       cat("Hot-path file: ", file, "\n");
       # Remove the file afterwards? Especially useful if you only
       # want to redefine functions, and patch once.  This way you
       # can also see what files has successfully been source():ed.
       if (removeAfter)
     }, error = function(ex) {
       cat("Ignored error when sourcing:\n");
} # patchCode()

Then in your batch code something like this:

while (!converged) {
   # Patch the code every iteration.
   patchCode(path="hot/", removeAfter=TRUE)
   # The rest of your code here

# Put the code you want to call at the end, in a directory
# of its own.
patchCode(path="hot/onFinally/", removeAfter=TRUE)
# End of your batch code



Duncan Murdoch wrote:
> Jean Eid wrote:
>>This is probably a weird question but I need to know if there is a way...
>>I run an R batch job without saving the variables at each step to the
>>disk.   Is there a way to invoke another session of R and link it to the
>>same environment for read only.
>>The problem is that I am running optim with every step getting the
>>parameters into the global env using <<- However, I forgot to issue a
>>save(list=ls(),...) right after so I can load and see how the parameters
>>are changing. It's been couple of days and it is still running so I am
>>hoping that I can invoke another session of R and link it to the
>>environment of the batch session. Does this sound totally ridiculous ?
>>it is a debian machine with R 2.1.1
> If you happened to have compiled R with debug information, you might be 
> able to use gdb or another debugger to examine variables in the running 
> process, but you probably didn't, and it's probably easier to kill the 
> job, fix it, and start it again, than it would be to learn how to see 
> the active variables using gdb.
> Duncan Murdoch
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list