ulrike niemann
ulrike niemann

Reputation: 55

officer - save document - dont overwrite

Is it possible to add a parameter like overwrite = FALSE when printing rpptx-Objects? I want to avoid to overwrite an possible existing file.

# write a rdocx object in a docx file ----
file <- tempfile(fileext = ".pptx")
doc <- read_pptx()
print(doc, target = file) # <- overwrites an existing file without message

I try do add parameter, but it overwrites existing files without warning or error.

print(doc, target = file, overwrite = FALSE)

Upvotes: 1

Views: 55

Answers (1)

Till
Till

Reputation: 6663

You could write a little function around print() that checks if the file exists and stop()s, if it does.

library(officer)

# Define function.
print_no_overwrite <- function(doc, target) {
  if (file.exists(target)) stop("File already exists!")
    print(doc, target)
  }

# Create test.pptx
doc <- read_pptx()
print(doc, target = "test.pptx")

# Fails because file already exists.
print_no_overwrite(doc, "test.pptx")
#> Error in print_no_overwrite(doc, "test.pptx"): File already exists!

# Works because it's a new file name.
print_no_overwrite(doc, "test2.pptx")

Upvotes: 1

Related Questions