1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
#' @name writeDataTable
#' @title Write to a worksheet as an Excel table
#' @description Write to a worksheet and format as an Excel table
#' @param wb A Workbook object containing a
#' worksheet.
#' @param sheet The worksheet to write to. Can be the worksheet index or name.
#' @param x A dataframe.
#' @param startCol A vector specifying the starting column to write df
#' @param startRow A vector specifying the starting row to write df
#' @param xy An alternative to specifying startCol and startRow individually.
#' A vector of the form c(startCol, startRow)
#' @param colNames If `TRUE`, column names of x are written.
#' @param rowNames If `TRUE`, row names of x are written.
#' @param row.names,col.names Deprecated, please use `rowNames`, `colNames` instead
#' @param tableStyle Any excel table style name or "none" (see "formatting" vignette).
#' @param tableName name of table in workbook. The table name must be unique.
#' @param headerStyle Custom style to apply to column names.
#' @param withFilter If `TRUE` or `NA`, columns with have filters in the first row.
#' @param keepNA If `TRUE`, NA values are converted to #N/A (or `na.string`, if not NULL) in Excel, else NA cells will be empty.
#' @param na.string If not NULL, and if `keepNA` is `TRUE`, NA values are converted to this string in Excel.
#' @param sep Only applies to list columns. The separator used to collapse list columns to a character vector e.g. sapply(x$list_column, paste, collapse = sep).
#' @param stack If `TRUE` the new style is merged with any existing cell styles. If FALSE, any
#' existing style is replaced by the new style.
#' \cr\cr
#' \cr**The below options correspond to Excel table options:**
#' \cr
#' \if{html}{\figure{tableoptions.png}{options: width="40\%" alt="Figure: table_options.png"}}
#' \if{latex}{\figure{tableoptions.pdf}{options: width=7cm}}
#'
#' @param firstColumn logical. If TRUE, the first column is bold
#' @param lastColumn logical. If TRUE, the last column is bold
#' @param bandedRows logical. If TRUE, rows are colour banded
#' @param bandedCols logical. If TRUE, the columns are colour banded
#' @details columns of x with class Date/POSIXt, currency, accounting,
#' hyperlink, percentage are automatically styled as dates, currency, accounting,
#' hyperlinks, percentages respectively.
#' @seealso [addWorksheet()]
#' @seealso [writeData()]
#' @seealso [removeTable()]
#' @seealso [getTables()]
#' @export
#' @examples
#' ## see package vignettes for further examples.
#'
#' #####################################################################################
#' ## Create Workbook object and add worksheets
#' wb <- createWorkbook()
#' addWorksheet(wb, "S1")
#' addWorksheet(wb, "S2")
#' addWorksheet(wb, "S3")
#'
#'
#' #####################################################################################
#' ## -- write data.frame as an Excel table with column filters
#' ## -- default table style is "TableStyleMedium2"
#'
#' writeDataTable(wb, "S1", x = iris)
#'
#' writeDataTable(wb, "S2",
#' x = mtcars, xy = c("B", 3), rowNames = TRUE,
#' tableStyle = "TableStyleLight9"
#' )
#'
#' df <- data.frame(
#' "Date" = Sys.Date() - 0:19,
#' "T" = TRUE, "F" = FALSE,
#' "Time" = Sys.time() - 0:19 * 60 * 60,
#' "Cash" = paste("$", 1:20), "Cash2" = 31:50,
#' "hLink" = "https://CRAN.R-project.org/",
#' "Percentage" = seq(0, 1, length.out = 20),
#' "TinyNumbers" = runif(20) / 1E9, stringsAsFactors = FALSE
#' )
#'
#' ## openxlsx will apply default Excel styling for these classes
#' class(df$Cash) <- c(class(df$Cash), "currency")
#' class(df$Cash2) <- c(class(df$Cash2), "accounting")
#' class(df$hLink) <- "hyperlink"
#' class(df$Percentage) <- c(class(df$Percentage), "percentage")
#' class(df$TinyNumbers) <- c(class(df$TinyNumbers), "scientific")
#'
#' writeDataTable(wb, "S3", x = df, startRow = 4, rowNames = TRUE, tableStyle = "TableStyleMedium9")
#'
#' #####################################################################################
#' ## Additional Header Styling and remove column filters
#'
#' writeDataTable(wb,
#' sheet = 1, x = iris, startCol = 7, headerStyle = createStyle(textRotation = 45),
#' withFilter = FALSE
#' )
#'
#'
#' #####################################################################################
#' ## Save workbook
#' ## Open in excel without saving file: openXL(wb)
#' \dontrun{
#' saveWorkbook(wb, "writeDataTableExample.xlsx", overwrite = TRUE)
#' }
#'
#'
#'
#'
#'
#' #####################################################################################
#' ## Pre-defined table styles gallery
#'
#' wb <- createWorkbook(paste0("tableStylesGallery.xlsx"))
#' addWorksheet(wb, "Style Samples")
#' for (i in 1:21) {
#' style <- paste0("TableStyleLight", i)
#' writeDataTable(wb,
#' x = data.frame(style), sheet = 1,
#' tableStyle = style, startRow = 1, startCol = i * 3 - 2
#' )
#' }
#'
#' for (i in 1:28) {
#' style <- paste0("TableStyleMedium", i)
#' writeDataTable(wb,
#' x = data.frame(style), sheet = 1,
#' tableStyle = style, startRow = 4, startCol = i * 3 - 2
#' )
#' }
#'
#' for (i in 1:11) {
#' style <- paste0("TableStyleDark", i)
#' writeDataTable(wb,
#' x = data.frame(style), sheet = 1,
#' tableStyle = style, startRow = 7, startCol = i * 3 - 2
#' )
#' }
#'
#' ## openXL(wb)
#' \dontrun{
#' saveWorkbook(wb, file = "tableStylesGallery.xlsx", overwrite = TRUE)
#' }
#'
writeDataTable <- function(
wb,
sheet,
x,
startCol = 1,
startRow = 1,
xy = NULL,
colNames = TRUE,
rowNames = FALSE,
tableStyle = openxlsx_getOp("tableStyle", "TableStyleLight9"),
tableName = NULL,
headerStyle = openxlsx_getOp("headerStyle"),
withFilter = openxlsx_getOp("withFilter", TRUE),
keepNA = openxlsx_getOp("keepNA", FALSE),
na.string = openxlsx_getOp("na.string"),
sep = ", ",
stack = FALSE,
firstColumn = openxlsx_getOp("firstColumn", FALSE),
lastColumn = openxlsx_getOp("lastColumn", FALSE),
bandedRows = openxlsx_getOp("bandedRows", TRUE),
bandedCols = openxlsx_getOp("bandedCols", FALSE),
col.names,
row.names
) {
op <- get_set_options()
on.exit(options(op), add = TRUE)
## increase scipen to avoid writing in scientific
if (!missing(row.names)) {
warning("Please use 'rowNames' instead of 'row.names'", call. = FALSE)
row.names <- rowNames
}
if (!missing(col.names)) {
warning("Please use 'colNames' instead of 'col.names'", call. = FALSE)
colNames <- col.names
}
# Set NULLs
withFilter <- withFilter %||% TRUE
keepNA <- keepNA %||% FALSE
firstColumn <- firstColumn %||% FALSE
lastColumn <- lastColumn %||% FALSE
bandedRows <- bandedRows %||% TRUE
bandedCols <- bandedCols %||% FALSE
withFilter <- withFilter %||% TRUE
if (!is.null(xy)) {
if (length(xy) != 2) {
stop("xy parameter must have length 2")
}
startCol <- xy[[1]]
startRow <- xy[[2]]
}
# Assert parameters
assert_class(wb, "Workbook")
assert_class(x, "data.frame")
assert_true_false(colNames)
assert_true_false(rowNames)
assert_class(headerStyle, "Style", or_null = TRUE)
assert_true_false(withFilter)
assert_character1(sep)
assert_true_false(firstColumn)
assert_true_false(lastColumn)
assert_true_false(bandedRows)
assert_true_false(bandedCols)
if (is.null(tableName)) {
tableName <- sprintf("Table%i", length(wb$tables) + 3L)
} else {
tableName <- wb$validate_table_name(tableName)
}
## convert startRow and startCol
if (!is.numeric(startCol)) {
startCol <- convertFromExcelRef(startCol)
}
startRow <- as.integer(startRow)
## Coordinates for each section
if (rowNames) {
x <- cbind(data.frame("row names" = rownames(x)), as.data.frame(x))
}
## If 0 rows append a blank row
tableStyle <- validate_StyleName(tableStyle)
## header style
if (inherits(headerStyle, "Style")) {
addStyle(
wb = wb,
sheet = sheet,
style = headerStyle,
rows = startRow,
cols = 0:(ncol(x) - 1L) + startCol,
gridExpand = TRUE
)
}
showColNames <- colNames
if (colNames) {
colNames <- colnames(x)
assert_unique(colNames, case_sensitive = FALSE)
## zero char names are invalid
char0 <- nchar(colNames) == 0
if (any(char0)) {
colNames[char0] <- colnames(x)[char0] <- paste0("Column", which(char0))
}
# Compatibility with MS Excel: throw warning if a table column name exceeds
# the length of 255 chars.
char_over255 <- nchar(colNames) > 255
if (any(char_over255)) {
warning_msg <- sprintf(
"Column name exceeds 255 chars, possible incompatibility with MS Excel. Index: %s",
toString(which(char_over255)))
warning(warning_msg)
}
} else {
colNames <- paste0("Column", seq_along(x))
names(x) <- colNames
}
## If zero rows, append an empty row (prevent XML from corrupting)
if (nrow(x) == 0) {
x <- rbind(
as.data.frame(x),
matrix("", nrow = 1, ncol = ncol(x), dimnames = list(character(), colnames(x)))
)
names(x) <- colNames
}
ref1 <- paste0(convert_to_excel_ref(cols = startCol, LETTERS = LETTERS), startRow)
ref2 <- paste0(convert_to_excel_ref(cols = startCol + ncol(x) - 1, LETTERS = LETTERS), startRow + nrow(x))
ref <- paste(ref1, ref2, sep = ":")
## check not overwriting another table
wb$check_overwrite_tables(
sheet = sheet,
new_rows = c(startRow, startRow + nrow(x) - 1L + 1L), ## + header
new_cols = c(startCol, startCol + ncol(x) - 1L)
)
## column class styling
# consider not using lowercase and instead use inherits(x, class)
colClasses <- lapply(x, function(x) tolower(class(x)))
classStyles(
wb,
sheet = sheet,
startRow = startRow,
startCol = startCol,
colNames = TRUE,
nRow = nrow(x),
colClasses = colClasses,
stack = stack
)
## write data to worksheet
wb$writeData(
df = x,
colNames = TRUE,
sheet = sheet,
startRow = startRow,
startCol = startCol,
colClasses = colClasses,
hlinkNames = NULL,
keepNA = keepNA,
na.string = na.string,
list_sep = sep
)
## replace invalid XML characters
colNames <- replaceIllegalCharacters(colNames)
## create table.xml and assign an id to worksheet tables
wb$buildTable(
sheet = sheet,
colNames = colNames,
ref = ref,
showColNames = showColNames,
tableStyle = tableStyle,
tableName = tableName,
withFilter = withFilter[1],
totalsRowCount = 0L,
showFirstColumn = firstColumn[1],
showLastColumn = lastColumn[1],
showRowStripes = bandedRows[1],
showColumnStripes = bandedCols[1]
)
}
|