pushTemporaryFile

0th

Percentile

Appends a temporary suffix to the pathname

Appends a temporary suffix to the pathname and, optionally, renames an existing file accordingly. In combination with popTemporaryFile(), this method is useful for creating a file/writing data to file atomically, by first writing to a temporary file which is the renamed. If for some reason the generation of the file was interrupted, for instance by a user interrupt or a power failure, then it is only the temporary file that is incomplete.

Keywords
utilities, programming, IO
Usage
## S3 method for class 'default':
pushTemporaryFile(filename, path=NULL, suffix=".tmp", isFile=FALSE, ..., verbose=FALSE)
Arguments
filename
The filename of the file.
path
The path of the file.
suffix
The suffix to be appended.
isFile
If TRUE, the file must exist and will be renamed on the file system. If FALSE, it is only the pathname string that will be
...
Not used.
verbose
A logical or Verbose.
Details

If isFile is FALSE, the pathname where the suffix of the temporary pathname has been added is returned. If isFile is TRUE, the file is also renamed. Then, if the file does not exists or it was not successfully renamed, an exception is thrown.

Value

  • Returns the pathname with the suffix appended.

See Also

popTemporaryFile().

Aliases
  • pushTemporaryFile.default
  • pushTemporaryFile
Examples
createAtomically <- function(pathname, ...) {
  cat("Pathname: ", pathname, "", sep="");

  # Generate a file atomically, i.e. the file will either be
  # complete or not created at all.  If interrupted while
  # writing, only a temporary file will exist/remain.
  pathnameT <- pushTemporaryFile(pathname);
  cat("Temporary pathname: ", pathnameT, "", sep="");

  cat(file=pathnameT, "This file was created atomically:
");
  for (kk in 1:10) {
    cat(file=pathnameT, kk, "", append=TRUE);
    Sys.sleep(0.1);
  }
  cat(file=pathnameT, "END OF FILE
", append=TRUE);

  # Rename the temporary file
  pathname <- popTemporaryFile(pathnameT);

  pathname;
} # createAtomically()


pathname <- tempfile();

tryCatch({
  # Try to interrupt the process while writing...
  pathname <- createAtomically(pathname);
}, interrupt=function(intr) {
  str(intr);
})

# ...and this will throw an exception
bfr <- readLines(pathname);
cat(bfr, sep="");
Documentation reproduced from package R.utils, version 1.7.3, License: LGPL (>= 2.1)

Community examples

Looks like there are no examples yet.