11setOldClass(
2- c(" unit" , " simpleUnit" , " unit.list" , " gpar" , " grob" , " gtable" , " gridifyObject" , " gridifyCells" , " gridifyCell" )
2+ c(
3+ " unit" ,
4+ " simpleUnit" ,
5+ " unit.list" ,
6+ " gpar" ,
7+ " grob" ,
8+ " gtable" ,
9+ " gridifyObject" ,
10+ " gridifyCells" ,
11+ " gridifyCell"
12+ )
313)
414setClassUnion(" unitOrSimpleUnit" , c(" unit" , " simpleUnit" , " unit.list" ))
515
@@ -13,6 +23,7 @@ setClassUnion("unitOrSimpleUnit", c("unit", "simpleUnit", "unit.list"))
1323# ' @slot widths A `grid::unit()` call specifying the widths of the columns.
1424# ' @slot margin A `grid::unit()` specifying the margins around the object.
1525# ' @slot global_gpar A `grid::gpar()` object specifying the global graphical parameters.
26+ # ' @slot background A string with background colour.
1627# ' @slot adjust_height A logical value indicating whether to adjust the height of the object.
1728# ' Only applies for cells with height defined in cm, mm, inch or lines units.
1829# ' @slot object A grob object.
@@ -27,28 +38,46 @@ setClass(
2738 widths = " unitOrSimpleUnit" ,
2839 margin = " unitOrSimpleUnit" ,
2940 global_gpar = " gpar" ,
41+ background = " character" ,
3042 adjust_height = " logical" ,
3143 object = " gridifyObject" ,
3244 cells = " gridifyCells"
3345 )
3446)
3547
3648setValidity(" gridifyLayout" , function (object ) {
37- if (! is(object @ margin , " unit" )) stop(" The 'margin' argument must be of type 'unit'." )
38- if (length(object @ margin ) != 4 ) stop(" The 'margin' argument must be a vector of length 4." )
49+ if (! is(object @ margin , " unit" )) {
50+ stop(" The 'margin' argument must be of type 'unit'." )
51+ }
52+ if (length(object @ margin ) != 4 ) {
53+ stop(" The 'margin' argument must be a vector of length 4." )
54+ }
55+ if (! ((length(object @ background ) == 1 ) && is.character(object @ background ))) {
56+ stop(" The 'background' argument must be a string." )
57+ }
3958 if (length(object @ heights ) != object @ nrow ) {
40- stop(" heights has to have the same length as number of rows (nrow) in gridifyLayout." )
59+ stop(
60+ " heights has to have the same length as number of rows (nrow) in gridifyLayout."
61+ )
4162 }
4263 if (length(object @ widths ) != object @ ncol ) {
43- stop(" widths has to have the same length as number of cols (ncol) in gridifyLayout." )
64+ stop(
65+ " widths has to have the same length as number of cols (ncol) in gridifyLayout."
66+ )
4467 }
4568
4669 if (! all(object @ object @ row < = object @ nrow )) {
47- stop(sprintf(" gridifyObject row value has to be less or equal to nrow: %s." , object @ nrow ))
70+ stop(sprintf(
71+ " gridifyObject row value has to be less or equal to nrow: %s." ,
72+ object @ nrow
73+ ))
4874 }
4975
5076 if (! all(object @ object @ col < = object @ ncol )) {
51- stop(sprintf(" gridifyObject col value has to be less or equal to ncol: %s." , object @ ncol ))
77+ stop(sprintf(
78+ " gridifyObject col value has to be less or equal to ncol: %s." ,
79+ object @ ncol
80+ ))
5281 }
5382
5483 cells_rows <- unlist(sapply(object @ cells @ cells , function (e ) e @ row ))
@@ -57,24 +86,36 @@ setValidity("gridifyLayout", function(object) {
5786 if (! all(cells_rows < = object @ nrow )) {
5887 stop(c(
5988 " All cells rows have to be less or equal to nrow." ,
60- " \n Number of rows: " , object @ nrow ,
61- " \n Maximum row number called in a cell: " , max(cells_rows )
89+ " \n Number of rows: " ,
90+ object @ nrow ,
91+ " \n Maximum row number called in a cell: " ,
92+ max(cells_rows )
6293 ))
6394 }
6495 if (! all(cells_cols < = object @ ncol )) {
6596 stop(c(
6697 " All cells cols have to be less or equal to ncol" ,
67- " \n Number of rows: " , object @ ncol ,
68- " \n Maximum row number called in a cell: " , max(cells_cols )
98+ " \n Number of rows: " ,
99+ object @ ncol ,
100+ " \n Maximum row number called in a cell: " ,
101+ max(cells_cols )
69102 ))
70103 }
71104
72105 occupied <- NULL
73106 overlap <- NULL
74107
75108 for (cell in object @ cells @ cells ) {
76- r_range <- if (length(cell @ row ) == 2 ) seq(min(cell @ row ), max(cell @ row )) else cell @ row
77- c_range <- if (length(cell @ col ) == 2 ) seq(min(cell @ col ), max(cell @ col )) else cell @ col
109+ r_range <- if (length(cell @ row ) == 2 ) {
110+ seq(min(cell @ row ), max(cell @ row ))
111+ } else {
112+ cell @ row
113+ }
114+ c_range <- if (length(cell @ col ) == 2 ) {
115+ seq(min(cell @ col ), max(cell @ col ))
116+ } else {
117+ cell @ col
118+ }
78119
79120 for (r in r_range ) {
80121 for (c in c_range ) {
@@ -110,6 +151,7 @@ setValidity("gridifyLayout", function(object) {
110151# ' Must be a vector of length 4, one element for each margin, with values in order for top, right, bottom, left.
111152# ' @param global_gpar A call to `grid::gpar()` specifying the global graphical parameters.
112153# ' Default is `grid::gpar()`.
154+ # ' @param background A string with background colour. Default `grid::get.gpar()$fill`.
113155# ' @param adjust_height A logical value indicating whether to automatically adjust the height of the object to
114156# ' make sure all of the text elements around the output do not overlap.
115157# ' This only applies for rows with height defined in cm, mm, inch or lines units. Default is TRUE.
@@ -128,6 +170,7 @@ setValidity("gridifyLayout", function(object) {
128170# ' widths = grid::unit(1, "npc"),
129171# ' margin = grid::unit(c(t = 0.1, r = 0.1, b = 0.1, l = 0.1), units = "npc"),
130172# ' global_gpar = grid::gpar(),
173+ # ' background = grid::get.gpar()$fill,
131174# ' adjust_height = FALSE,
132175# ' object = gridifyObject(row = 2, col = 1),
133176# ' cells = gridifyCells(
@@ -156,6 +199,7 @@ setValidity("gridifyLayout", function(object) {
156199# ' heights = grid::unit(c(3, 0.5, 1, 3), c("cm", "cm", "null", "cm")),
157200# ' widths = grid::unit(1, "npc"),
158201# ' global_gpar = global_gpar,
202+ # ' background = grid::get.gpar()$fill,
159203# ' margin = margin,
160204# ' adjust_height = FALSE,
161205# ' object = gridifyObject(row = 3, col = 1),
@@ -174,22 +218,26 @@ setValidity("gridifyLayout", function(object) {
174218# ' set_cell("subtitle", "SUBTITLE") %>%
175219# ' set_cell("footer", "FOOTER")
176220gridifyLayout <- function (
177- nrow ,
178- ncol ,
179- heights ,
180- widths ,
181- margin ,
182- global_gpar = grid :: gpar(),
183- adjust_height = TRUE ,
184- object ,
185- cells ) {
186- new(" gridifyLayout" ,
221+ nrow ,
222+ ncol ,
223+ heights ,
224+ widths ,
225+ margin ,
226+ global_gpar = grid :: gpar(),
227+ background = grid :: get.gpar()$ fill ,
228+ adjust_height = TRUE ,
229+ object ,
230+ cells
231+ ) {
232+ new(
233+ " gridifyLayout" ,
187234 nrow = nrow ,
188235 ncol = ncol ,
189236 heights = heights ,
190237 widths = widths ,
191238 margin = margin ,
192239 global_gpar = global_gpar ,
240+ background = background ,
193241 adjust_height = adjust_height ,
194242 object = object ,
195243 cells = cells
@@ -230,9 +278,15 @@ setClass(
230278)
231279
232280setValidity(" gridifyCell" , function (object ) {
233- if (min(object @ row ) < 1 || ! all(object @ row %% 1 == 0 )) stop(" cell row has to be positive integer." )
234- if (min(object @ col ) < 1 || ! all(object @ col %% 1 == 0 )) stop(" cell col has to be positive integer." )
235- if (length(object @ text ) > 1 ) stop(" cell text has to be a string." )
281+ if (min(object @ row ) < 1 || ! all(object @ row %% 1 == 0 )) {
282+ stop(" cell row has to be positive integer." )
283+ }
284+ if (min(object @ col ) < 1 || ! all(object @ col %% 1 == 0 )) {
285+ stop(" cell col has to be positive integer." )
286+ }
287+ if (length(object @ text ) > 1 ) {
288+ stop(" cell text has to be a string." )
289+ }
236290
237291 TRUE
238292})
@@ -281,16 +335,17 @@ setValidity("gridifyCell", function(object) {
281335# ' gpar = grid::gpar()
282336# ' )
283337gridifyCell <- function (
284- row ,
285- col ,
286- text = character (0 ),
287- mch = Inf ,
288- x = 0.5 ,
289- y = 0.5 ,
290- hjust = 0.5 ,
291- vjust = 0.5 ,
292- rot = 0 ,
293- gpar = grid :: gpar()) {
338+ row ,
339+ col ,
340+ text = character (0 ),
341+ mch = Inf ,
342+ x = 0.5 ,
343+ y = 0.5 ,
344+ hjust = 0.5 ,
345+ vjust = 0.5 ,
346+ rot = 0 ,
347+ gpar = grid :: gpar()
348+ ) {
294349 new(
295350 " gridifyCell" ,
296351 row = row ,
@@ -320,13 +375,19 @@ setClass(
320375)
321376
322377setValidity(" gridifyCells" , function (object ) {
323- if (length(object @ cells ) == 0 ) stop(" gridifyCells can not be empty." )
324- if (length(names(object @ cells )) != length(object @ cells )) stop(" All elements in gridifyCells have to be named." )
378+ if (length(object @ cells ) == 0 ) {
379+ stop(" gridifyCells can not be empty." )
380+ }
381+ if (length(names(object @ cells )) != length(object @ cells )) {
382+ stop(" All elements in gridifyCells have to be named." )
383+ }
325384 if (length(unique(names(object @ cells ))) != length(object @ cells )) {
326385 stop(" All elements in gridifyCells must have unique names." )
327386 }
328387
329- if (! all(vapply(object @ cells , function (e ) is(e , " gridifyCell" ), logical (1 )))) {
388+ if (
389+ ! all(vapply(object @ cells , function (e ) is(e , " gridifyCell" ), logical (1 )))
390+ ) {
330391 stop(" All elements in gridifyCells have to be gridifyCell." )
331392 }
332393
@@ -392,8 +453,12 @@ setClass(
392453)
393454
394455setValidity(" gridifyObject" , function (object ) {
395- if (min(object @ row ) < 1 || ! all(object @ row %% 1 == 0 )) stop(" cell row has to be positive integer." )
396- if (min(object @ col ) < 1 || ! all(object @ col %% 1 == 0 )) stop(" cell col has to be positive integer." )
456+ if (min(object @ row ) < 1 || ! all(object @ row %% 1 == 0 )) {
457+ stop(" cell row has to be positive integer." )
458+ }
459+ if (min(object @ col ) < 1 || ! all(object @ col %% 1 == 0 )) {
460+ stop(" cell col has to be positive integer." )
461+ }
397462
398463 TRUE
399464})
@@ -518,20 +583,26 @@ setValidity("gridifyClass", function(object) {
518583# ' )
519584# ' )
520585gridify <- function (
521- object = grid :: nullGrob(),
522- layout ,
523- elements = list (),
524- ... ) {
586+ object = grid :: nullGrob(),
587+ layout ,
588+ elements = list (),
589+ ...
590+ ) {
525591 # Check the classes of the inputs
526592 accepted_classes <- c(" grob" , " ggplot" , " flextable" , " gt_tbl" , " formula" )
527593 if (! (inherits(object , accepted_classes ))) {
528- stop(sprintf(" object argument of gridify has to be one of %s class." , paste(accepted_classes , collapse = " , " )))
594+ stop(sprintf(
595+ " object argument of gridify has to be one of %s class." ,
596+ paste(accepted_classes , collapse = " , " )
597+ ))
529598 }
530599
531600 if (! (inherits(layout , " gridifyLayout" ) || is.function(layout ))) {
532601 stop(" layout argument of gridify has to be of gridifyLayout class." )
533602 }
534- if (! is.list(elements )) stop(" elements argument of gridify has to be a list." )
603+ if (! is.list(elements )) {
604+ stop(" elements argument of gridify has to be a list." )
605+ }
535606
536607 if (is.function(layout )) {
537608 layout <- layout()
@@ -549,7 +620,10 @@ gridify <- function(
549620 }
550621
551622 if (inherits(object , " flextable" )) {
552- if (requireNamespace(" flextable" ) && (utils :: packageVersion(" flextable" ) > = " 0.8.0" )) {
623+ if (
624+ requireNamespace(" flextable" ) &&
625+ (utils :: packageVersion(" flextable" ) > = " 0.8.0" )
626+ ) {
553627 object <- flextable :: gen_grob(object )
554628 } else {
555629 stop(" Please install flextable >= 0.8.0 to use it in gridify, as it depends on flextable::gen_grob." )
@@ -576,7 +650,8 @@ gridify <- function(
576650 }
577651
578652 # Create a new gridify object
579- new(" gridifyClass" ,
653+ new(
654+ " gridifyClass" ,
580655 object = object ,
581656 layout = layout ,
582657 elements = if (length(elements )) elements else list ()
0 commit comments