GitHub

R-CMD-check Codecov test coverage pharmaverse gridify Badge CRAN status status: active

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 (with rtables.officer).

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 by design. By unifying tables and figures into scalable vector graphics, gridify locks the layout so it cannot break across environments — no shifting columns, no reflowing text. The output is stable while the text stays clear, searchable, and copy-able at any zoom level.

Installation

You can install the newest release version from CRAN:

install.packages("gridify")

Or you can install the newest development version 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. Print the gridify object to see empty cells
  5. 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", # gt font size is in pixels # Multiply points by 96/72 to get pixels table.font.size = 10 * 96 / 72, table.font.names = "sans" ) # Use `gridify()` to create a `gridify` object gridify_object <- gridify( object = tab, # Choose a layout (predefined or custom) layout = pharma_layout_base( margin = grid::unit(c(0.5, 0.5, 0.5, 0.5), "inches"), global_gpar = grid::gpar(fontfamily = "sans", fontsize = 10) ) ) # Print the `gridify` object to see empty cells gridify_object # Use `set_cell()` to fill in the various text elements in the layout (headers etc.) gridify_object_fill <- gridify_object |> set_cell("header_left_1", "My Company") |> set_cell("header_left_2", " / ") |> set_cell("header_left_3", " ") |> set_cell("header_right_1", "CONFIDENTIAL") |> set_cell("header_right_2", " ") |> set_cell("header_right_3", "Data Cut-off: YYYY-MM-DD") |> set_cell("output_num", " xx.xx.xx") |> set_cell("title_1", "

Read the original on github.com ↗