This function is a wrapper for openxlsx::addWorksheet() and openxlsx::writeData(), but also does some extra formatting via more openxlsx functions.

add_sheet(
  wb,
  data,
  sheetName = NULL,
  colWidth = "auto",
  colWidthMin = 5,
  colWidthMax = 25,
  gridLines = FALSE,
  freezefirstRow = TRUE,
  freezefirstCol = FALSE,
  addFilters = TRUE,
  rowheight = 15,
  textwrap = TRUE,
  dateformat = "yyyy-mm-dd",
  datetimeformat = "yyyy-mm-dd hh:mm:ss"
)

Arguments

wb

A Workbook object to attach the new worksheet

data

Object to be written. For classes supported look at the examples in openxlsx.

sheetName

The name of the worksheet that is to be created and have data written into. Defaults to the data object name if left empty.

colWidth

For all columns: column width. Can either be Excel column width units or "auto" for automatic sizing

colWidthMin

For all columns: minimum column width

colWidthMax

For all columns: maximum column width

gridLines

If FALSE, the worksheet grid lines will be hidden

freezefirstRow

If TRUE, freezes the first row

freezefirstCol

If TRUE, freezes the first row

addFilters

If TRUE, adds filters to worksheet columns

rowheight

Row height in Excel row height units

textwrap

If TRUE, all cells are formatted via text wrap so their content never spills into adjacent empty cells

dateformat

If not "none" (default is "yyyy-mm-dd"), all date-formatted columns in data are explicitly formatted with numFmt = "yyyy-mm-dd"

datetimeformat

If not "none" (default is "yyyy-mm-dd hh:mm:ss"), all datetime-formatted columns in data are explicitly formatted with numFmt = "yyyy-mm-dd hh:mm:ss"

Examples

library(BioMathR)
wb1 <- create_wb()
wb1 <- add_sheet(wb = wb1, data = mtcars)

library(dplyr, warn.conflicts = FALSE)
wb2 <- create_wb() %>%
   add_sheet(mtcars, "The cars sheet")