createTempDir <- function() {
tmpdir <- tempdir()
tmpfile1 <- tempfile(tmpdir=tmpdir)
tmpfile2 <- tempfile(tmpdir=tmpdir)
writeLines("First file", tmpfile1)
writeLines("Second file", tmpfile2)
return(tmpdir)
}
newTempFile <- function(tmpdir) {
writeLines("Third file", tempfile(tmpdir=tmpdir))
}
# \donttest{
tmpdir <- createTempDir()
overwriteDir(tmpdir, action="ask")
## overwrite: delete the directory and create it a new
tmpdir <- createTempDir()
fileCount <- length(dir(tmpdir))
dir(tmpdir) ## two files should be there
overwriteDir(tmpdir, action="overwrite")
newTempFile(tmpdir)
dir(tmpdir) ## now there should be only one file
stopifnot(length(dir(tmpdir))==1)
## append: append files, and overwrite if a file of the same name is there
overwriteDir(tmpdir, action="append")
newTempFile(tmpdir)
dir(tmpdir) ## a new file is written
stopifnot(length(dir(tmpdir))==2)
## no: no action, and returns FALSE
noRes <- overwriteDir(tmpdir, action="no")
stopifnot(!noRes)
# }
Run the code above in your browser using DataLab