Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing overwriting of files when using save() or save.image()

Tags:

file

r

I am trying to find a way to stop accidental overwriting of files when using the save() and save.image() function in R.

like image 789
ggg Avatar asked Feb 04 '23 09:02

ggg


1 Answers

Use file.exists() to test if the file is there, and if it is, append a string to the name.

Edit:

Thanks Marek, I'll expand on your idea a bit... he could add this to deal with both save() and save.image()

SafeSave <- function( ..., file=stop("'file' must be specified"), overwrite=FALSE, save.fun=save) {
  if ( file.exists(file) & !overwrite ) stop("'file' already exists")
  save.fun(..., file=file)
}

I would not overwrite save... if source() was used in a REPL session, users may not be aware of the function overwrite.

like image 104
Vince Avatar answered Feb 06 '23 01:02

Vince