Skip to content

Commit cbcf90d

Browse files
authored
release 0.7.3 (#3)
release 0.7.3
1 parent 8a896e2 commit cbcf90d

24 files changed

+962
-327
lines changed

.Rbuildignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
^_pkgdown\.yml$
1111
^\.github$
1212
^\.devcontainer$
13+
^Rplots\.pdf$
1314

15+
^doc$
16+
^Meta$

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,5 @@ inst/doc
7575
*.docx
7676
*.ppt
7777
*.pptx
78+
/doc/
79+
/Meta/

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: gridify
33
Title: Enrich Figures and Tables with Custom Headers and Footers and More
4-
Version: 0.7.2
4+
Version: 0.7.3
55
Authors@R: c(
66
person("Maciej", "Nasinski", , "Maciej.Nasinski@ucb.com", role = c("aut", "cre")),
77
person("Alexandra", "Wall", , "Alexandra.Wall@ucb.com", role = "aut"),

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# gridify 0.7.3
2+
3+
* Improved `show_spec` method for `gridifyLayout`.
4+
* New feature of background colour for layouts, by the new `background` argument.
5+
16
# gridify 0.7.2
27

38
* First open-source release.

R/ansi_colour.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#'
66
#' @return a character string with ANSI codes surrounding it.
77
#' @keywords internal
8-
#' @examples
9-
#' \dontrun{
10-
#' cat(gridify:::coloured_print("This is a test", colour = "blue"))
11-
#' }
128
coloured_print <- function(text, colour = "green") {
139
# Note ANSI colour codes
1410
colour_codes <- list(

R/complex_layout.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#' @param margin A unit object specifying the margins around the output. Default is 10% of the output area on all sides.
99
#' @param global_gpar A gpar object specifying the global graphical parameters.
1010
#' Must be the result of a call to `grid::gpar()`.
11+
#' @param background A string specifying the background fill colour.
12+
#' Default `grid::get.gpar()$fill` for a white background.
1113
#' @param scales A string, either `"free"` or `"fixed"`.
1214
#' By default, `"fixed"` ensures that text elements (titles, footers, etc.)
1315
#' retain a static height, preventing text overlap while maintaining a
@@ -76,9 +78,11 @@
7678
#' @rdname complex_layout
7779
#' @export
7880
complex_layout <- function(
79-
margin = grid::unit(c(t = 0.1, r = 0.1, b = 0.1, l = 0.1), units = "npc"),
80-
global_gpar = grid::gpar(),
81-
scales = c("fixed", "free")) {
81+
margin = grid::unit(c(t = 0.1, r = 0.1, b = 0.1, l = 0.1), units = "npc"),
82+
global_gpar = grid::gpar(),
83+
background = grid::get.gpar()$fill,
84+
scales = c("fixed", "free")
85+
) {
8286
scales <- match.arg(scales, c("fixed", "free"))
8387

8488
heights <- if (scales == "free") {
@@ -96,6 +100,7 @@ complex_layout <- function(
96100
heights = heights,
97101
widths = grid::unit(rep(1, 3) / 3, units = "npc"),
98102
global_gpar = global_gpar,
103+
background = background,
99104
margin = margin,
100105
adjust_height = TRUE,
101106
object = gridifyObject(row = 4, col = c(1, 3)),

R/gridify-classes.R

Lines changed: 124 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
setOldClass(
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
)
414
setClassUnion("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

3648
setValidity("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-
"\nNumber of rows: ", object@nrow,
61-
"\nMaximum row number called in a cell: ", max(cells_rows)
89+
"\nNumber of rows: ",
90+
object@nrow,
91+
"\nMaximum 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-
"\nNumber of rows: ", object@ncol,
68-
"\nMaximum row number called in a cell: ", max(cells_cols)
98+
"\nNumber of rows: ",
99+
object@ncol,
100+
"\nMaximum 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")
176220
gridifyLayout <- 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

232280
setValidity("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
#' )
283337
gridifyCell <- 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

322377
setValidity("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

394455
setValidity("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
#' )
520585
gridify <- 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

Comments
 (0)