/
PivotStyles.R
407 lines (382 loc) · 20.6 KB
/
PivotStyles.R
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#' R6 class that defines a collection of styles.
#'
#' @description
#' The `PivotStyles` class is a collection of `PivotStyle` objects.
#' It defines all of the base styles needed to style/theme a
#' pivot table. It also defines the names of the styles that are used for
#' styling the different parts of the pivot table.
#'
#' @docType class
#' @importFrom R6 R6Class
#' @export
#' @format \code{\link{R6Class}} object.
#' @examples
#' pt <- PivotTable$new()
#' # ...
#' pivotStyles <- PivotStyles$new(pt, themeName="compact")
#' pivotStyles$addStyle(styleName="MyNewStyle", list(
#' font="0.75em arial",
#' padding="2px",
#' border="1px solid lightgray",
#' "vertical-align"="middle",
#' "text-align"="center",
#' "font-weight"="bold",
#' "background-color"="#F2F2F2"
#' ))
PivotStyles <- R6::R6Class("PivotStyles",
public = list(
#' @description
#' Create a new `PivotStyles` object.
#' @param parentPivot The pivot table that this `PivotStyles`
#' instance belongs to.
#' @param themeName A theme name to represent this collection of styles.
#' @param allowExternalStyles Default `FALSE`, which means this `PivotStyles`
#' object checks that style names specified for styling the different
#' parts of the pivot table must exist in the styles collection. If they do
#' not an error will occur. Specify `TRUE` to disable this check, e.g. if
#' the style definitions are not managed by `pivottabler` but instead
#' in an external system.
#' @return A new `PivotStyles` object.
initialize = function(parentPivot, themeName=NULL, allowExternalStyles=FALSE) {
if(parentPivot$argumentCheckMode > 0) {
checkArgument(parentPivot$argumentCheckMode, FALSE, "PivotStyles", "initialize", parentPivot, missing(parentPivot), allowMissing=FALSE, allowNull=FALSE, allowedClasses="PivotTable")
checkArgument(parentPivot$argumentCheckMode, FALSE, "PivotStyles", "initialize", themeName, missing(themeName), allowMissing=TRUE, allowNull=TRUE, allowedClasses="character")
checkArgument(parentPivot$argumentCheckMode, FALSE, "PivotStyles", "initialize", allowExternalStyles, missing(allowExternalStyles), allowMissing=TRUE, allowNull=TRUE, allowedClasses="logical")
}
private$p_parentPivot <- parentPivot
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$new", "Creating new Pivot Styles...")
private$p_theme <- themeName
private$p_allowExternalStyles <- allowExternalStyles
private$p_styles <- list()
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$new", "Created new Pivot Styles.")
},
#' @description
#' Check whether a style with the specified style name exists in the
#' collection.
#' @param styleName The name of the style.
#' @return `TRUE` if a style with the specified name exists, `FALSE` otherwise.
isExistingStyle = function(styleName=NULL) {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "isExistingStyle", styleName, missing(styleName), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$isExistingStyle", "Checking style exists...", list(styleName=styleName))
styleExists <- styleName %in% names(private$p_styles)
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$isExistingStyle", "Checked style exists.")
return(invisible(styleExists))
},
#' @description
#' Retrieve a style with the specified style name.
#' @param styleName The name of the style.
#' @return A `PivotStyle` object.
getStyle = function(styleName=NULL) {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "getStyle", styleName, missing(styleName), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$getStyle", "Getting style...", list(styleName=styleName))
style <- private$p_styles[[styleName]]
if(is.null(style)) {
stop(paste0("PivotStyles$getStyle(): No style exists with the name '", styleName, "'"), call. = FALSE)
}
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$getStyle", "Got style.")
return(invisible(style))
},
#' @description
#' Add a new style to the collection of styles.
#' @param styleName The name of the new style.
#' @param declarations CSS style declarations in the form of a list, e.g.
#' `list("font-weight"="bold", "color"="#0000FF")`
#' @return The newly created `PivotStyle` object.
addStyle = function(styleName=NULL, declarations= NULL) {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "addStyle", styleName, missing(styleName), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "addStyle", declarations, missing(declarations), allowMissing=TRUE, allowNull=TRUE, allowedClasses="list", allowedListElementClasses="character")
}
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$addStyle", "Adding style...", list(styleName=styleName))
if(styleName %in% names(private$p_styles)) {
stop(paste0("PivotStyles$addStyle(): A style already exists",
" with the name '", styleName, "'. styleName must unique."), call. = FALSE)
}
style <- PivotStyle$new(private$p_parentPivot, styleName, declarations)
private$p_styles[[styleName]] <- style
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$addStyle", "Added style.")
return(invisible(style))
},
#' @description
#' Create a copy of a style with the specified name and store
#' it in the collection.
#' @param styleName The name of the style to copy.
#' @param newStyleName The name for the new style.
#' @return The newly created `PivotStyle` object.
copyStyle = function(styleName=NULL, newStyleName=NULL) {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "copyStyle", styleName, missing(styleName), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "copyStyle", newStyleName, missing(newStyleName), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$copyStyle", "Copying style...", list(styleName=styleName, newStyleName=newStyleName))
style <- self$getStyle(styleName=styleName)
newStyle <- self$addStyle(styleName=newStyleName, declarations=style$declarations)
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$copyStyle", "Copied style.")
return(invisible(newStyle))
},
#' @description
#' Get a style definition in the form of a CSS rule.
#' @param styleName The name of the style.
#' @param selector A CSS selector, used to select the element(s) to be styled.
#' @return The style declarations in the form of a CSS rule,
#' i.e. selector { property-name1: property-value1,
#' property-name2: property-value2, ... }
#' e.g. div { font-weight: bold, color: #0000FF }
asCSSRule = function(styleName=NULL, selector=NULL) {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "asCSSRule", styleName, missing(styleName), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "asCSSRule", selector, missing(selector), allowMissing=TRUE, allowNull=TRUE, allowedClasses="character")
}
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$asCSSRule", "Getting style as CSS rule...", list(styleName=styleName))
style <- self$getStyle(styleName)
cssRule <- style$asCSSRule(selector=selector)
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$asCSSRule", "Got style as CSS rule.")
return(invisible(cssRule))
},
#' @description
#' Get a style definition in the form of a named CSS style.
#' @param styleName The name of the style.
#' @param styleNamePrefix A prefix to prepend to the style name.
#' @return The style declarations in the form of named CSS style,
#' i.e. .prefix-stylename { property-name1: property-value1,
#' property-name2: property-value2, ... }
#' e.g. .pvt1Cell { font-weight: bold, color: #0000FF }
asNamedCSSStyle = function(styleName=NULL, styleNamePrefix=NULL) {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "asNamedCSSStyle", styleName, missing(styleName), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "asNamedCSSStyle", styleNamePrefix, missing(styleNamePrefix), allowMissing=TRUE, allowNull=TRUE, allowedClasses="character")
}
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$asNamedCSSStyle", "Getting style as named CSS rule...", list(styleName=styleName, styleNamePrefix=styleNamePrefix))
style <- self$getStyle(styleName)
cssRule <- style$asNamedCSSStyle(styleNamePrefix=styleNamePrefix)
if(private$p_parentPivot$traceEnabled==TRUE) private$p_parentPivot$trace("PivotStyles$asNamedCSSStyle", "Got style as named CSS rule.")
return(invisible(cssRule))
},
#' @description
#' Return the contents of this object as a list for debugging.
#' @return A list of various object properties.
asList = function() {
lst <- list()
if(length(private$p_styles) > 0) {
groupNames <- names(private$p_styles)
for (i in 1:length(private$p_styles)) {
groupName <- groupNames[i]
lst[[groupName]] = private$p_styles[[groupName]]$asList()
}
}
return(invisible(lst))
},
#' @description
#' Return the contents of this object as JSON for debugging.
#' @return A JSON representation of various object properties.
asJSON = function() {
if (!requireNamespace("jsonlite", quietly = TRUE)) {
stop("The jsonlite package is needed to convert to JSON. Please install the jsonlite package.", call. = FALSE)
}
jsonliteversion <- utils::packageDescription("jsonlite")$Version
if(numeric_version(jsonliteversion) < numeric_version("1.1")) {
stop("Version 1.1 or above of the jsonlite package is needed to convert to JSON. Please install an updated version of the jsonlite package.", call. = FALSE)
}
return(jsonlite::toJSON(self$asList()))
},
#' @description
#' Return a representation of this object as a character value.
#' @param seperator A character value to use when concatenating
#' multiple styles.
#' @return A character summary of various object properties.
asString = function(seperator=", ") {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "asString", seperator, missing(seperator), allowMissing=TRUE, allowNull=FALSE, allowedClasses="character")
}
cstr <- ""
if(length(private$p_styles)>0) {
for(i in 1:length(private$p_styles)) {
cg <- private$p_styles[[i]]
sep <- ""
if(i > 1) { sep <- seperator }
cstr <- paste0(cstr, sep, cg$asString())
}
}
return(cstr)
}
),
active = list(
#' @field count The number of `PivotStyle` objects in this
#' `PivotStyles` collection.
count = function(value) { return(invisible(length(private$p_styles))) },
#' @field theme A theme name to represent this collection of styles.
theme = function(value) { return(invisible(private$p_theme)) },
#' @field styles A list containing the `PivotStyle` objects in this
#' `PivotStyles` collection.
styles = function(value) { return(invisible(private$p_styles)) },
#' @field allowExternalStyles Default `FALSE`, which means this `PivotStyles`
#' object checks that style names specified for styling the different
#' parts of the pivot table must exist in the styles collection. If they do
#' not an error will occur. Specify `TRUE` to disable this check, e.g. if
#' the style definitions are not managed by `pivottabler` but instead
#' in an external system.
allowExternalStyles = function(value) {
if(missing(value)) return(invisible(private$p_allowExternalStyles))
else {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "allowExternalStyles", value, missing(value), allowMissing=FALSE, allowNull=FALSE, allowedClasses="logical")
}
private$p_allowExternalStyles <- value
return(invisible())
}
},
#' @field tableStyle The name of the style for the HTML table element.
tableStyle = function(value) {
if(missing(value)) return(invisible(private$p_tableStyle))
else {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "tableStyle", value, missing(value), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_allowExternalStyles==FALSE) {
if(!(value %in% names(private$p_styles))) stop(paste0("PivotStyles$tableStyle: '", value, "' style not found in styles list."))
}
private$p_tableStyle <- value
return(invisible())
}
},
#' @field rootStyle The name of the style for the HTML cell at the top left of
#' the pivot table.
rootStyle = function(value) {
if(missing(value)) return(invisible(private$p_rootStyle))
else {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "rootStyle", value, missing(value), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_allowExternalStyles==FALSE) {
if(!(value %in% names(private$p_styles))) stop(paste0("PivotStyles$rootStyle: '", value, "' style not found in styles list."))
}
private$p_rootStyle <- value
return(invisible())
}
},
#' @field rowHeaderStyle The name of the style for the row headers in the pivot
#' table.
rowHeaderStyle = function(value) {
if(missing(value)) return(invisible(private$p_rowHeaderStyle))
else {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "rowHeaderStyle", value, missing(value), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_allowExternalStyles==FALSE) {
if(!(value %in% names(private$p_styles))) stop(paste0("PivotStyles$rowHeaderStyle: '", value, "' style not found in styles list."))
}
private$p_rowHeaderStyle <- value
return(invisible())
}
},
#' @field colHeaderStyle The name of the style for the column headers in the
#' pivot table.
colHeaderStyle = function(value) {
if(missing(value)) return(invisible(private$p_colHeaderStyle))
else {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "colHeaderStyle", value, missing(value), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_allowExternalStyles==FALSE) {
if(!(value %in% names(private$p_styles))) stop(paste0("PivotStyles$colHeaderStyle: '", value, "' style not found in styles list."))
}
private$p_colHeaderStyle <- value
return(invisible())
}
},
#' @field outlineRowHeaderStyle The name of the style for the outline row
#' headers in the pivot table.
outlineRowHeaderStyle = function(value) {
if(missing(value)) return(invisible(private$p_outlineRowHeaderStyle))
else {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "outlineRowHeaderStyle", value, missing(value), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_allowExternalStyles==FALSE) {
if(!(value %in% names(private$p_styles))) stop(paste0("PivotStyles$outlineRowHeaderStyle: '", value, "' style not found in styles list."))
}
private$p_outlineRowHeaderStyle <- value
return(invisible())
}
},
#' @field outlineColHeaderStyle The name of the style for the outline column
#' headers in the pivot table.
outlineColHeaderStyle = function(value) {
if(missing(value)) return(invisible(private$p_outlineColHeaderStyle))
else {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "outlineColHeaderStyle", value, missing(value), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_allowExternalStyles==FALSE) {
if(!(value %in% names(private$p_styles))) stop(paste0("PivotStyles$outlineColHeaderStyle: '", value, "' style not found in styles list."))
}
private$p_outlineColHeaderStyle <- value
return(invisible())
}
},
#' @field cellStyle The name of the cell style for the non-total cells in the
#' body of the pivot table.
cellStyle = function(value) {
if(missing(value)) return(invisible(private$p_cellStyle))
else {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "cellStyle", value, missing(value), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_allowExternalStyles==FALSE) {
if(!(value %in% names(private$p_styles))) stop(paste0("PivotStyles$cellStyle: '", value, "' style not found in styles list."))
}
private$p_cellStyle <- value
return(invisible())
}
},
#' @field outlineCellStyle The name of the cell style for the non-total
#' outline cells in the body of the pivot table.
outlineCellStyle = function(value) {
if(missing(value)) return(invisible(private$p_outlineCellStyle))
else {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "outlineCellStyle", value, missing(value), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_allowExternalStyles==FALSE) {
if(!(value %in% names(private$p_styles))) stop(paste0("PivotStyles$outlineCellStyle: '", value, "' style not found in styles list."))
}
private$p_outlineCellStyle <- value
return(invisible())
}
},
#' @field totalStyle The name of the cell style for the total cells in the pivot
#' table.
totalStyle = function(value) {
if(missing(value)) return(invisible(private$p_totalStyle))
else {
if(private$p_parentPivot$argumentCheckMode > 0) {
checkArgument(private$p_parentPivot$argumentCheckMode, FALSE, "PivotStyles", "totalStyle", value, missing(value), allowMissing=FALSE, allowNull=FALSE, allowedClasses="character")
}
if(private$p_allowExternalStyles==FALSE) {
if(!(value %in% names(private$p_styles))) stop(paste0("PivotStyles$totalStyle: '", value, "' style not found in styles list."))
}
private$p_totalStyle <- value
return(invisible())
}
}
),
private = list(
p_parentPivot = NULL,
p_theme = NULL,
p_allowExternalStyles = FALSE,
p_styles = NULL,
p_tableStyle = NULL,
p_rootStyle = NULL,
p_rowHeaderStyle = NULL,
p_colHeaderStyle = NULL,
p_outlineRowHeaderStyle = NULL,
p_outlineColHeaderStyle = NULL,
p_cellStyle = NULL,
p_outlineCellStyle = NULL,
p_totalStyle = NULL
)
)