Skip to contents

Save a workbook to file

Usage

wb_save(wb, file = NULL, overwrite = TRUE, path = NULL, flush = FALSE)

Arguments

wb

A wbWorkbook object to write to file

file

A path to save the workbook to

overwrite

If FALSE, will not overwrite when file already exists.

path

Deprecated argument. Please use file in new code.

flush

Experimental, streams the worksheet file to disk

Value

the wbWorkbook object, invisibly

Details

When saving a wbWorkbook to a file, memory usage may spike depending on the worksheet size. This happens because the entire XML structure is created in memory before writing to disk. The memory required depends on worksheet size, as XML files consist of character data and include additional overhead for validity checks.

The flush argument streams worksheet XML data directly to disk, avoiding the need to build the full XML tree in memory. This reduces memory usage but skips some XML validity checks. It also bypasses the pugixml functions that openxlsx2 uses, omitting certain preliminary sanity checks before writing. As the name suggests, the output is simply flushed to disk.

Examples

## Create a new workbook and add a worksheet
wb <- wb_workbook("Creator of workbook")
wb$add_worksheet(sheet = "My first worksheet")

## Save workbook to working directory
# \donttest{
wb_save(wb, file = temp_xlsx(), overwrite = TRUE)
# }