I am trying to find a way to stop accidental overwriting of files when using the save() and save.image() function in R.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With