[R] How to protect two jobs running on the same directory at the same time not to corrupt each other results:

Michel Lang michellang at gmail.com
Thu Feb 8 22:47:59 CET 2007


Am Donnerstag, 8. Februar 2007 21:33 schrieb Aldi Kraja:
> Is there any other solution better than creating separate directories in
> R? I am thinking if there is any option in R to create a unique id which
> has its own unique .Rdata, although in the same directory?

You could try using tempfile() in combination with basename(). From the 
helppage on tempfile():
    The names are very likely to be unique among calls to 'tempfile'
     in an R session and across simultaneous R sessions.  The filenames
     are guaranteed not to be currently in use.

So this is my suggestion for the generation of a useful, unique id in your 
working dir:

prefix <- paste("var_", var, sep = "")
while(file.exists(id <- basename(tempfile(pattern = prefix)))) {}
save(result, file = paste(id, ".Rdata", sep = ""))

Hope this helps.

Michel Lang



More information about the R-help mailing list