Learn R Programming

gridify

Overview

In the pharmaceutical industry, and many other fields that rely heavily on data reporting, there is often a need to create figures and tables with specific graphical arrangements. These could be titles, subtitles, captions, footnotes, and other text elements that provide important context to the data being shown.

However, creating the headers and footers etc. and correctly positioning them around the output can be challenging, often requiring fine-tuning. This can be time-consuming and can lead to inconsistencies in the way the figures and tables are presented across different projects.

gridify builds on the base R grid package and makes it easy to add flexible and customizable information around a figure or table using a pre-defined or custom layout. The gridify package works with all of the following input types, creating consistency when using various different inputs:

grob, gtable, ggplot, flextable, gt, base R plots (by formula)
Whilst rtables are not directly supported, we can use rtables with gridify by first converting them to flextable.

As gridify is based on the graphical tool grid, any figure or table inputs are converted to a grob object in gridify and the result of using gridify is always a graphical image.

Installation

If gridify is not yet on CRAN, you can install it from Pharmaverse GitHub (example):

# install.packages("remotes")
remotes::install_github("pharmaverse/gridify", build_manual = TRUE)

Example

The workflow of the package is as follows:

  1. Create your object (ggplot, gt etc.)
  2. Choose a layout (predefined or custom). Use get_layouts() to see the predefined options
  3. Use gridify() to create a gridify object
  4. Use set_cell() to fill in the various text elements in the layout (headers, footers etc.)

The following example uses a table created by the gt package and the gridify layout pharma_layout_base().

library(gridify)
# install.packages("gt")
# gt needs gtable
# install.packages("gtable")
library(gt)
# (to use |> version 4.1.0 of R is required, for lower versions we recommend %>% from magrittr)

tab <- gt::gt(head(mtcars, n = 10))  |>
  gt::tab_options(
    table.width = gt::pct(100),
    data_row.padding = gt::px(10),
    table_body.hlines.color = "white",
    table.font.size = 12
  )

gridify_object <- gridify(
  object = tab,
  layout = pharma_layout_base(
    margin = grid::unit(c(0.5, 0.5, 0.5, 0.5), "inches"),
    global_gpar = grid::gpar(fontfamily = "serif", fontsize = 10)
  )
)

gridify_object

gridify_object_fill <- gridify_object |>
  set_cell("header_left_1", "My Company") |>
  set_cell("header_left_2", "<PROJECT> / <INDICATION>") |>
  set_cell("header_left_3", "<STUDY>") |>
  set_cell("header_right_1", "CONFIDENTIAL") |>
  set_cell("header_right_2", "<Draft or Final>") |>
  set_cell("header_right_3", "Data Cut-off: YYYY-MM-DD") |>
  set_cell("output_num", "<Table> xx.xx.xx") |>
  set_cell("title_1", "<Title 1>") |>
  set_cell("title_2", "<Title 2>") |>
  set_cell("title_3", "<Optional Title 3>") |>
  set_cell("by_line", "By: <GROUP>, <optionally: Demographic parameters>") |>
  set_cell("note", "<Note or Footnotes>") |>
  set_cell("references", "<References:>") |>
  set_cell("footer_left", "Program: <PROGRAM NAME>, YYYY-MM-DD at HH:MM") |>
  set_cell("footer_right", "Page xx of nn") |>
  set_cell("watermark", "DRAFT")

gridify_object_fill

print(gridify_object_fill)

Note: Get the image using
export_to(gridify_object_fill, to = "mypng.png", res = 300, width = 2300, height = 1900)

Documentation Guide

For more information please visit the following vignettes:

  • Getting Started vignette("gridify", package = "gridify") - A case study in how the above example is constructed.
  • Simple Examples vignette("simple_examples", package = "gridify") - A showcase of implementations of gridify for the various possible inputs.
  • Multi-Page Examples vignette("multi_page_examples", package = "gridify") - Showing how to use gridify in more complex situations e.g a for-loop for multiple results.
  • Create Custom Layout vignette("create_custom_layout", package = "gridify") - An explanation on how to create a custom layout to use in gridify.
  • Transparency of gridify vignette("transparency", package = "gridify") - How to extract the raw grid code to reproduce a gridify object.

Related packages

Other packages exist which add headers, footers, and other elements to figures and tables; most of the input classes to gridify already support these features. However, gridify was created not to supersede these, but to be used in conjunction with, in a way that is flexible for all use cases and consistent across various inputs.

Contributing

Interested in contributing? Check out the contributing guidelines, CONTRIBUTING.md. Please note that this project is released with a Code of Conduct, CODE_OF_CONDUCT.md. By contributing to this project, you agree to abide by its terms.

Acknowledgments

Along with the authors and contributors, thanks to the following people for their support:

Alberto Montironi, Jonathan Bleier, Cynthia McShea, Nils Penard, Oswald Dallimore, Laetitia Lemoine, Daniel Vicencio Perez, Richard Abdy

Copy Link

Version

Install

install.packages('gridify')

Version

0.7.4

License

Apache License 2.0

Maintainer

Maciej Nasinski

Last Published

August 28th, 2025

Functions in gridify (0.7.4)

get_layouts

Get the gridify layouts
gridifyObject-class

gridifyObject class
gridifyObject

Create a gridifyObject
gridifyLayout

Create a gridifyLayout
layout_issue

Template for Layout Issues Note
gridifyCells-class

gridifyCells class
gridifyLayout-class

gridifyLayout class
show,gridifyClass-method

Show method for gridifyClass
show_cells

Show the cells in a gridify object or layout
print,gridifyClass-method

Print method for gridifyClass
show_spec

Show the layout specifications of a gridifyClass or gridifyLayout
pharma_layout_base

Base Function for Pharma Layouts
show_layout

Show the layout in a given gridify object or layout
simple_layout

Simple Layout for a gridify object
pharma_layout_letter

Pharma Layout (Letter) for a gridify object
show,gridifyLayout-method

Show method for gridifyLayout
set_cell

Add text elements to a gridify cell
gpar_call

Convert grid::gpar to a call
export_to

Export gridify objects to a file
gridifyCell-class

gridifyCell class
complex_layout

Complex Layout for a gridify object
gridify

Create a gridify object
gridifyCell

Create a gridifyCell
gridifyClass-class

gridifyClass class
gridifyCells

Create a gridifyCells
pharma_layout

Pharma Layouts
pharma_layout_A4

Pharma Layout (A4) for a gridify object
coloured_print

Add ANSI colour to a string
grid_unit_type

Wrapper for grid::unitType which supports older R versions
gpar_args

Get grid::gpar arguments