|
| 1 | +#' @name xls_theme |
| 2 | +#' |
| 3 | +#' @title Constructor function for xls themes |
| 4 | +#' |
| 5 | +#' @description |
| 6 | +#' This function creates an xls theme for styling exported tables. |
| 7 | +#' All its arguments must be `openxlsx` Style objects. |
| 8 | +#' |
| 9 | +#' @param title Style for the title |
| 10 | +#' @param col_header Style for the columns header |
| 11 | +#' @param character Default style for data cells |
| 12 | +#' @param footnote1 Style for footnote1 |
| 13 | +#' @param footnote2 Style for footnote2 |
| 14 | +#' @param footnote3 Style for footnote3 |
| 15 | +#' @param mergedcell Style for merged cells |
| 16 | +#' @param ... Other (named) custom styles |
| 17 | +#' |
| 18 | +#' @return a named list of class xls_theme, whose elements are `openxlsx` Style objects. |
| 19 | +#' @export |
| 20 | +#' |
| 21 | +#' @seealso \code{\link[tablexlsx:xls_theme_plain]{xls_theme_plain()}}, |
| 22 | +#' \code{\link[tablexlsx:xls_theme_default]{xls_theme_default()}} |
| 23 | +#' |
| 24 | +#' @examples |
| 25 | +#' my_theme <- xls_theme( |
| 26 | +#' title = openxlsx::createStyle(), |
| 27 | +#' col_header = openxlsx::createStyle(), |
| 28 | +#' character = openxlsx::createStyle(), |
| 29 | +#' footnote1 = openxlsx::createStyle(), |
| 30 | +#' footnote2 = openxlsx::createStyle(), |
| 31 | +#' footnote3 = openxlsx::createStyle(), |
| 32 | +#' mergedcell = openxlsx::createStyle() |
| 33 | +#' ) |
| 34 | +#' |
| 35 | +#' \dontrun{ |
| 36 | +#' toxlsx(object = iris, path = tempdir(), theme = my_theme) |
| 37 | +#' } |
| 38 | +xls_theme <- function(title, |
| 39 | + col_header, |
| 40 | + character, |
| 41 | + footnote1, |
| 42 | + footnote2, |
| 43 | + footnote3, |
| 44 | + mergedcell, |
| 45 | + ...) { |
| 46 | + theme <- list(title = title, |
| 47 | + col_header = col_header, |
| 48 | + character = character, |
| 49 | + footnote1 = footnote1, |
| 50 | + footnote2 = footnote2, |
| 51 | + footnote3 = footnote3, |
| 52 | + mergedcell = mergedcell, |
| 53 | + ...) |
| 54 | + class(theme) <- "xls_theme" |
| 55 | + assert_xls_theme(theme) |
| 56 | + theme |
| 57 | +} |
| 58 | + |
| 59 | +#' @name xls_theme_plain |
| 60 | +#' |
| 61 | +#' @title Constructor function for a plain xls theme |
| 62 | +#' |
| 63 | +#' @description |
| 64 | +#' This function is a wrapper around [xls_theme()] that creates an xls theme for styling exported tables. |
| 65 | +#' It defines a simple theme whith no special formatting. |
| 66 | +#' All its arguments must be `openxlsx` Style objects. |
| 67 | +#' |
| 68 | +#' @param title Style for the title |
| 69 | +#' @param col_header Style for the columns header |
| 70 | +#' @param character Default style for data cells |
| 71 | +#' @param footnote1 Style for footnote1 |
| 72 | +#' @param footnote2 Style for footnote2 |
| 73 | +#' @param footnote3 Style for footnote3 |
| 74 | +#' @param mergedcell Style for merged cells |
| 75 | +#' @param ... Other (named) custom styles |
| 76 | +#' |
| 77 | +#' @return a named list of class xls_theme, whose elements are `openxlsx` Style objects. |
| 78 | +#' @export |
| 79 | +#' |
| 80 | +#' @seealso \code{\link[tablexlsx:xls_theme]{xls_theme()}}, |
| 81 | +#' \code{\link[tablexlsx:xls_theme_default]{xls_theme_default()}} |
| 82 | +#' |
| 83 | +#' @examples |
| 84 | +#' # plain theme |
| 85 | +#' xls_theme_plain() |
| 86 | +#' |
| 87 | +#' # plain theme with title in bold |
| 88 | +#' my_theme <- xls_theme_plain(title = openxlsx::createStyle(textDecoration = "bold")) |
| 89 | +#' |
| 90 | +#' \dontrun{ |
| 91 | +#' toxlsx(object = iris, path = tempdir(), theme = my_theme) |
| 92 | +#' } |
| 93 | +xls_theme_plain = function( |
| 94 | + title = openxlsx::createStyle(), |
| 95 | + col_header = openxlsx::createStyle(), |
| 96 | + character = openxlsx::createStyle(), |
| 97 | + footnote1 = openxlsx::createStyle(), |
| 98 | + footnote2 = openxlsx::createStyle(), |
| 99 | + footnote3 = openxlsx::createStyle(), |
| 100 | + mergedcell = openxlsx::createStyle(), |
| 101 | + ... |
| 102 | +) { |
| 103 | + xls_theme( |
| 104 | + title = title, |
| 105 | + col_header = col_header, |
| 106 | + character = character, |
| 107 | + footnote1 = footnote1, |
| 108 | + footnote2 = footnote2, |
| 109 | + footnote3 = footnote3, |
| 110 | + mergedcell = mergedcell, |
| 111 | + ... |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +#' @name xls_theme_default |
| 116 | +#' |
| 117 | +#' @title Constructor function for the default xls theme |
| 118 | +#' |
| 119 | +#' @description |
| 120 | +#' This function is a wrapper around [xls_theme()] that creates an xls theme for styling exported tables. |
| 121 | +#' It defines a theme whith sensible default formatting values. |
| 122 | +#' It also defines custom styles for "number", "decimal" and "percent column types. |
| 123 | +#' All its arguments must be `openxlsx` Style objects. |
| 124 | +#' |
| 125 | +#' @param title Style for the title |
| 126 | +#' @param col_header Style for the columns header |
| 127 | +#' @param character Default style for data cells |
| 128 | +#' @param number Style for columns in number format |
| 129 | +#' @param decimal Style for columns in decimal format |
| 130 | +#' @param percent Style for columns in percent format |
| 131 | +#' @param footnote1 Style for footnote1 |
| 132 | +#' @param footnote2 Style for footnote2 |
| 133 | +#' @param footnote3 Style for footnote3 |
| 134 | +#' @param mergedcell Style for merged cells |
| 135 | +#' @param ... Other (named) custom styles |
| 136 | +#' |
| 137 | +#' @return a named list of class xls_theme, whose elements are `openxlsx` Style objects. |
| 138 | +#' @export |
| 139 | +#' |
| 140 | +#' @seealso \code{\link[tablexlsx:xls_theme]{xls_theme()}}, |
| 141 | +#' \code{\link[tablexlsx:xls_theme_plain]{xls_theme_plain()}} |
| 142 | +#' |
| 143 | +#' @examples |
| 144 | +#' # default theme |
| 145 | +#' xls_theme_default() |
| 146 | +#' |
| 147 | +#' # default theme with title in italic |
| 148 | +#' my_theme <- xls_theme_default(title = openxlsx::createStyle(textDecoration = "italic")) |
| 149 | +#' |
| 150 | +#' \dontrun{ |
| 151 | +#' toxlsx(object = iris, path = tempdir(), theme = my_theme) |
| 152 | +#' } |
| 153 | +xls_theme_default = function( |
| 154 | + title = openxlsx::createStyle(fontSize = 16, textDecoration = "bold"), |
| 155 | + # For footnote1 |
| 156 | + footnote1 = openxlsx::createStyle(fontSize = 12), |
| 157 | + # For footnote2 |
| 158 | + footnote2 = openxlsx::createStyle(fontSize = 12), |
| 159 | + # For footnote3 |
| 160 | + footnote3 = openxlsx::createStyle(fontSize = 12), |
| 161 | + # For column headers |
| 162 | + col_header = openxlsx::createStyle( |
| 163 | + fontSize = 12, |
| 164 | + textDecoration = "bold", |
| 165 | + border = c("top", "bottom", "left", "right"), |
| 166 | + borderStyle = "thin", |
| 167 | + wrapText = TRUE, |
| 168 | + halign = "center" |
| 169 | + ), |
| 170 | + # For cells in character format |
| 171 | + character = openxlsx::createStyle( |
| 172 | + fontSize = 12, |
| 173 | + border = c("top", "bottom", "left", "right"), |
| 174 | + borderStyle = "thin" |
| 175 | + ), |
| 176 | + # For cells in number format (with thousands separator) |
| 177 | + number = openxlsx::createStyle( |
| 178 | + fontSize = 12, |
| 179 | + numFmt = "### ### ### ##0", |
| 180 | + border = c("top", "bottom", "left", "right"), |
| 181 | + borderStyle = "thin" |
| 182 | + ), |
| 183 | + # For cells in number format with decimals (and with thousands separator) |
| 184 | + decimal = openxlsx::createStyle( |
| 185 | + fontSize = 12, |
| 186 | + numFmt = "### ### ### ##0.0", |
| 187 | + border = c("top", "bottom", "left", "right"), |
| 188 | + borderStyle = "thin" |
| 189 | + ), |
| 190 | + # For cells in percentage format (centered) |
| 191 | + percent = openxlsx::createStyle( |
| 192 | + fontSize = 12, |
| 193 | + numFmt = "#0.0", |
| 194 | + border = c("top", "bottom", "left", "right"), |
| 195 | + borderStyle = "thin", |
| 196 | + halign = "center" |
| 197 | + ), |
| 198 | + mergedcell = openxlsx::createStyle( |
| 199 | + fontSize = 12, |
| 200 | + border = c("top", "bottom", "left", "right"), |
| 201 | + borderStyle = "thin", |
| 202 | + wrapText = TRUE, |
| 203 | + valign = "center", |
| 204 | + halign = "center" |
| 205 | + ), |
| 206 | + ... |
| 207 | + ) { |
| 208 | + xls_theme_plain( |
| 209 | + title = title, |
| 210 | + col_header = col_header, |
| 211 | + character = character, |
| 212 | + number = number, |
| 213 | + decimal = decimal, |
| 214 | + percent = percent, |
| 215 | + footnote1 = footnote1, |
| 216 | + footnote2 = footnote2, |
| 217 | + footnote3 = footnote3, |
| 218 | + mergedcell = mergedcell, |
| 219 | + ... |
| 220 | + ) |
| 221 | +} |
0 commit comments