formatXL | R Documentation |
This function takes a data.frame you want to save to an Excel file using the
package xlsx
and applies the formatting you want for the Excel file.
Warning: The arguments for this function are a lot of lists, and it
can be tricky to get the syntax exactly right, but I haven't yet come up with
a better way of doing things. If this isn't working, check that your lists
are laid out correctly and named correctly.
formatXL(
DF,
file,
sheet = NA,
colWidth = list(colNum = NULL, colName = NULL, width = NULL),
styles
)
DF |
input data.frame |
file |
file name (character) |
sheet |
sheet name (character). Defaults to the name of the supplied data.frame if no other name is supplied. |
colWidth |
A named list of
If colNum and colName are set to NULL, all columns will be set to the width listed. For any columns not specifically set or if width is set to NULL, reasonable guesses for column widths will be used. |
styles |
A list of lists (one list for every set of cells that you want to format) that contains the following named objects:
|
If this will create a new Excel file or if your current Excel file has no other sheets but this one, this will generate a message "Workbook has no sheets!" that I cannot seem to get rid of.
COMMON ERRORS or ERROR MESSAGES since this is a tad (ok, a LOT)
glitchy: styles
is meant to accommodate multiple sets of formatting,
so you have to have styles
be a list but then also each set of cells
that you're formatting must also be a list. This means that styles should
probably look like this in your code: styles = list(list(...))
If you set a style for a cell and then set another style for that same cell, the final style will be the 2nd one, not a combination of the two. For example, say you select row 1 and make it blue and bold, and then you add a border on the left side of all cells in column 4. The cell in row 1, column 4 will be the default Excel style of text plus having a border on the left side - not blue, bold, and border on left.
This does not return any R objects; it saves an Excel file.
data(iris)
iris$Date <- as.Date("2020-11-03")
iris$Money <- rnorm(nrow(iris), 20, 20)
formatXL(DF = iris, file = "test.xlsx", sheet = "iris1",
colWidth = list(width = 30),
styles = list(list(columns = 6,
numberFormat = "date"),
list(columns = 7,
numberFormat = "currency"),
list(rows = 0,
font = list(bold = TRUE, size = 18),
textposition = list(alignment = "middle",
wrapping = TRUE))))
formatXL(DF = iris, file = "test.xlsx", sheet = "iris2",
colWidth = list(colNum = c(1, 5, 6), width = c(25, 10, 30)),
styles = list(list(rows = 4,
font = list(color = "blue", bold = TRUE)),
list(rows = 8:9, columns = 3,
font = list(color = "red", italics = TRUE),
textposition = list(alignment = "center",
wrapping = TRUE),
fill = "dodgerblue3"),
list(rows = 0, columns = 1,
font = list(color = "purple", underline = TRUE),
textposition = list(alignment = "right",
wrapping = TRUE))))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.