Learn R Programming

openxlsx2 (version 0.3.1)

write_xlsx: write data to an xlsx file

Description

write a data.frame or list of data.frames to an xlsx file

Usage

write_xlsx(x, file, asTable = FALSE, ...)

Value

A workbook object

Arguments

x

object or a list of objects that can be handled by write_data() to write to file

file

xlsx file name

asTable

write using write_datatable as opposed to write_data

...

optional parameters to pass to functions:

  • wb_workbook()

  • wb_add_worksheet()

  • wb_add_data()

  • wb_freeze_pane

  • wb_save()

see details.

Details

Optional parameters are:

wb_workbook Parameters

  • creator A string specifying the workbook author

wb_add_worksheet() Parameters

  • sheetName Name of the worksheet

  • gridLines A logical. If FALSE, the worksheet grid lines will be hidden.

  • tabColour Colour of the worksheet tab. A valid colour (belonging to colours()) or a valid hex colour beginning with "#".

  • zoom A numeric between 10 and 400. Worksheet zoom level as a percentage.

write_data/write_datatable Parameters

  • startCol A vector specifying the starting column(s) to write df

  • startRow A vector specifying the starting row(s) to write df

  • xy An alternative to specifying startCol and startRow individually. A vector of the form c(startCol, startRow)

  • colNames or col.names If TRUE, column names of x are written.

  • rowNames or row.names If TRUE, row names of x are written.

  • na.string If not NULL NA values are converted to this string in Excel. Defaults to NULL.

freezePane Parameters

  • firstActiveRow Top row of active region to freeze pane.

  • firstActiveCol Furthest left column of active region to freeze pane.

  • firstRow If TRUE, freezes the first row (equivalent to firstActiveRow = 2)

  • firstCol If TRUE, freezes the first column (equivalent to firstActiveCol = 2)

colWidths Parameters

  • colWidths May be a single value for all columns (or "auto"), or a list of vectors that will be recycled for each sheet (see examples)

wb_save Parameters

  • overwrite Overwrite existing file (Defaults to TRUE as with write.table)

columns of x with class Date or POSIXt are automatically styled as dates and datetimes respectively.

See Also

wb_add_worksheet(), write_data()

Examples

Run this code

## write to working directory
write_xlsx(iris, file = temp_xlsx(), colNames = TRUE)

write_xlsx(iris,
  file = temp_xlsx(),
  colNames = TRUE
)

## Lists elements are written to individual worksheets, using list names as sheet names if available
l <- list("IRIS" = iris, "MTCATS" = mtcars, matrix(runif(1000), ncol = 5))
write_xlsx(l, temp_xlsx(), colWidths = c(NA, "auto", "auto"))

## different sheets can be given different parameters
write_xlsx(l, temp_xlsx(),
  startCol = c(1, 2, 3), startRow = 2,
  asTable = c(TRUE, TRUE, FALSE), withFilter = c(TRUE, FALSE, FALSE)
)

# specify column widths for multiple sheets
write_xlsx(l, temp_xlsx(), colWidths = 20)
write_xlsx(l, temp_xlsx(), colWidths = list(100, 200, 300))
write_xlsx(l, temp_xlsx(), colWidths = list(rep(10, 5), rep(8, 11), rep(5, 5)))

Run the code above in your browser using DataLab