| Title: | Clinical Table Styling Tools and Utilities |
|---|---|
| Description: | The primary motivation of this package is to take the things that are great about the R packages 'flextable' <https://davidgohel.github.io/flextable/> and 'officer' <https://davidgohel.github.io/officer/>, take the standard and complex pieces of formatting clinical tables for regulatory use, and simplify the tedious pieces. |
| Authors: | Mike Stackhouse [aut, cre] (ORCID: <https://orcid.org/0000-0001-6030-723X>), Ross Didenko [aut], Yevhenii Boiko [aut], Marat Zakirov [ctb], Roman Rogoza [ctb], Atorus Research, Inc. [cph], Incyte Corporation [cph] |
| Maintainer: | Mike Stackhouse <[email protected]> |
| License: | Apache License (>= 2) |
| Version: | 0.3.0.9000 |
| Built: | 2026-05-08 08:05:30 UTC |
| Source: | https://github.com/atorus-research/clinify |
Convert a flextable into a clintable object
as_clintable(x, page_by = NULL, group_by = NULL)as_clintable(x, page_by = NULL, group_by = NULL)
x |
A flextable object |
page_by |
A variable in the input dataframe to use for pagination |
group_by |
A variable which will be used for grouping and attached as a label above the table headers |
A clintable object
ft <- flextable::flextable(mtcars) as_clintable(ft)ft <- flextable::flextable(mtcars) as_clintable(ft)
This function allows you to attach specified titles, footnotes, or a footnote page into clintable or clindoc object. The input can be provided either as a list of character vectors, or pre-built flextable.
clin_add_titles(x, ls = NULL, ft = NULL) clin_add_footnotes(x, ls = NULL, ft = NULL) clin_add_footnote_page(x, ls = NULL, ft = NULL)clin_add_titles(x, ls = NULL, ft = NULL) clin_add_footnotes(x, ls = NULL, ft = NULL) clin_add_footnote_page(x, ls = NULL, ft = NULL)
x |
a clintable object |
ls |
a list of character vectors, no more than 2 elements to a vector |
ft |
A flextable object to use as the header |
When using the ls parameter, each element of the list can contain no more than two
elements within each character vector. In a title, a single element will align center.
In a footnote, a single element will align to the left. For both titles and footnotes,
two elements will align split down the middle, with the left side element aligning left
and the right side element aligning right. In a title, a single left aligned element,
provide a 2 element character vector with duplicate values.
A clintable object
clintable(mtcars) |> clin_add_titles( list( c("Left", "Right"), c("Just the middle") ) ) |> clin_add_footnotes( list( c( "Here's a footnote.", format(Sys.time(), "%H:%M %A, %B %d, %Y") ) ) ) |> clin_add_footnote_page( list( c( "Use when you have a lot of footnotes", "And you don't want to put them on every page" ) ) )clintable(mtcars) |> clin_add_titles( list( c("Left", "Right"), c("Just the middle") ) ) |> clin_add_footnotes( list( c( "Here's a footnote.", format(Sys.time(), "%H:%M %A, %B %d, %Y") ) ) ) |> clin_add_footnote_page( list( c( "Use when you have a lot of footnotes", "And you don't want to put them on every page" ) ) )
This function configures alternating pages on a clintable object.
clin_alt_pages(x, key_cols, col_groups)clin_alt_pages(x, key_cols, col_groups)
x |
A clintable object |
key_cols |
A character vector of variable names |
col_groups |
A list of character vectors of variable names |
A clintable object
ct <- clintable(mtcars) clin_alt_pages( ct, key_cols = c("mpg", "cyl", "hp"), col_groups = list( c("disp", "drat", "wt"), c("qsec", "vs", "am"), c("gear", "carb") ) )ct <- clintable(mtcars) clin_alt_pages( ct, key_cols = c("mpg", "cyl", "hp"), col_groups = list( c("disp", "drat", "wt"), c("qsec", "vs", "am"), c("gear", "carb") ) )
This function uses the applies the functionality flextable::keep_with_next()
by automatically building the row indices using some grouping variable. Each group
identified by the variable (i.e. when the value of the variable changes) will be set
as a "keep_with_next" group in Word. Using this functionality, Word will attempt not to
break that group across pages, enabling smoother pagination without having to do specific
calculations of page breaks.
clin_auto_page(x, group_var, when = c("change", "notempty"), drop = FALSE)clin_auto_page(x, group_var, when = c("change", "notempty"), drop = FALSE)
x |
A clintable object |
group_var |
A string containing a variable name of the input dataset used to calculate groups |
when |
Character string indicating when to apply padding:
|
drop |
Keep or drop the 'group_var“ variable |
A clintable object
clintable(mtcars) |> clin_auto_page("gear")clintable(mtcars) |> clin_auto_page("gear")
Extraction of flextable print method with special handling of clintable pages and
clin_col_widths(x, ...)clin_col_widths(x, ...)
x |
A clintable object |
... |
Named parameters where the names are columns in the flextable and the values are decimals representing the percent of total width of the table |
A clintable object
ct <- clintable(mtcars) ct <- clin_alt_pages( ct, key_cols = c("mpg", "cyl", "hp"), col_groups = list( c("disp", "drat", "wt"), c("qsec", "vs", "am"), c("gear", "carb") ) ) |> clin_col_widths(mpg = .2, cyl = .2, disp = .15, vs = .15) print(ct)ct <- clintable(mtcars) ct <- clin_alt_pages( ct, key_cols = c("mpg", "cyl", "hp"), col_groups = list( c("disp", "drat", "wt"), c("qsec", "vs", "am"), c("gear", "carb") ) ) |> clin_col_widths(mpg = .2, cyl = .2, disp = .15, vs = .15) print(ct)
This function allows you to apply column headers named arguments and character vectors. Separate elements of the character vector are converted to separate levels of the output table header. The in which the headers are applied goes from top to bottom, so if you provide 3 elements for a column header, the first element is applied to the top and the second to the bottom. If one variable has three levels and other variable only have one or two, the columns with less levels to the header will bind to the bottom. So a column with two levels will apply to the second and third row, and a column with one level with apply the bottom row. Spanners are determined using cells of the same text value, where horizontal and vertical merging is performed.
clin_column_headers(x, ...)clin_column_headers(x, ...)
x |
A clintable object |
... |
Named arguments providing the column header text. Separate levels of the header are determined using separate elements of a character vector. |
The same result can be achieved using column labels on the input dataframe to the clintable. If labels are present, header levels will be separated using the delimitter "||" within the label string.
A clintable object
clintable(iris) |> clin_column_headers( Sepal.Length = c("Flowers", "Sepal", "Length"), Sepal.Width = c("Flowers", "Sepal", "Width"), Petal.Length = c("Petal", "Length"), Petal.Width = c("Petal", "Width") )clintable(iris) |> clin_column_headers( Sepal.Length = c("Flowers", "Sepal", "Length"), Sepal.Width = c("Flowers", "Sepal", "Width"), Petal.Length = c("Petal", "Length"), Petal.Width = c("Petal", "Width") )
This function calculates the default table width based on the page width
and margins specified in the clinify_docx_default option.
clin_default_table_width()clin_default_table_width()
An rdocx object from the officer package
clin_default_table_width()clin_default_table_width()
Configure a clintable to table by a grouping variable, which will be used as a label
clin_group_by(x, group_by, caption_by = NULL, when = c("change", "notempty"))clin_group_by(x, group_by, caption_by = NULL, when = c("change", "notempty"))
x |
A clintable object |
group_by |
A character vector of variable names which will be used for grouping and attached as a label above the table headers |
caption_by |
A single element string of a variable name which will be used as a caption attached below the table body and above in the footer. Defaults to NULL. |
when |
Character string indicating how to identify groups and captions:
|
A clintable object
clintable(iris) |> clin_group_by("Species")clintable(iris) |> clin_group_by("Species")
Adds top padding to rows in a 'clintable“ based on changes in a grouping variable or non-empty values. Useful for visually separating groups in a table
clin_group_pad( x, pad_by, size = 9, when = c("change", "notempty"), drop = FALSE )clin_group_pad( x, pad_by, size = 9, when = c("change", "notempty"), drop = FALSE )
x |
A clintable |
pad_by |
A string indicating the column name used to detect group changes. |
size |
Numeric value for the base padding size (default is 9). |
when |
Character string indicating when to apply padding:
|
drop |
Keep or drop the padding variable used to identify padding locations |
A clintable object with modified padding.
ct <- clintable(mtcars) |> clin_group_pad('gear') ct <- clintable(mtcars) |> clin_group_pad('gear', size = 15)ct <- clintable(mtcars) |> clin_group_pad('gear') ct <- clintable(mtcars) |> clin_group_pad('gear', size = 15)
Configure pagination using a page variable
clin_page_by(x, page_by, max_rows = 10)clin_page_by(x, page_by, max_rows = 10)
x |
A clintable object |
page_by |
A variable in the input dataframe to use for pagination |
max_rows |
If no page_by, the maximum rows allowed per page |
A clintable object
dat <- mtcars dat["page"] <- c( rep(1, 10), rep(2, 10), rep(3, 10), c(4, 4) ) clintable(dat) |> clin_page_by("page") clintable(mtcars) |> clin_page_by(max_rows = 10)dat <- mtcars dat["page"] <- c( rep(1, 10), rep(2, 10), rep(3, 10), c(4, 4) ) clintable(dat) |> clin_page_by("page") clintable(mtcars) |> clin_page_by(max_rows = 10)
This helper function will find placeholder text and replace the fields of
the flextable object with the appropriate page number fields. The function
will search for the text {PAGE} and replace with the word field for current
pages, and {NUMPAGES} for total pages. This allows you to current and total
page fields within Word documents. Note that this is intended to be used in
the defaults for clinify_titles_default or clinify_footnotes_default.
clin_replace_pagenums(x)clin_replace_pagenums(x)
x |
A clintable object |
A clintable object
title <- new_title_footnote( list( # We'll add tools to automate paging c("Protocol: CDISCPILOT01", "Page {PAGE} of {NUMPAGES}"), c("Table 14-2.01"), c("Summary of Demographic and Baseline Characteristics") ), "titles" ) title <- clin_replace_pagenums(title) footnote <- new_title_footnote( list( # We'll add tools to automate paging c("Page {PAGE}", "Total Pages: {NUMPAGES}") ), "footnotes" ) footnote <- clin_replace_pagenums(footnote)title <- new_title_footnote( list( # We'll add tools to automate paging c("Protocol: CDISCPILOT01", "Page {PAGE} of {NUMPAGES}"), c("Table 14-2.01"), c("Summary of Demographic and Baseline Characteristics") ), "titles" ) title <- clin_replace_pagenums(title) footnote <- new_title_footnote( list( # We'll add tools to automate paging c("Page {PAGE}", "Total Pages: {NUMPAGES}") ), "footnotes" ) footnote <- clin_replace_pagenums(footnote)
clindoc objectThese functions handle the conversion of a clintable object into a clindoc
object.
clindoc(...) as_clindoc(x)clindoc(...) as_clindoc(x)
... |
|
x |
A |
as_clindoc() is intended for a single clintable object and extracts all
necessary title and footnote information to apply to the document
clindoc() can accept clintable objects as separate parameters or as a
list of clintable objects. If a single clintable is passed, this function
simply calls as_clindoc() internally.
When a multiple clintable objects are passed to clindoc(), titles and footnotes
should be applied directly to the clindoc object using clin_add_title(),
clin_add_footnote(), or clin_add_footnote_page(). Title and footnote information
on the individual clintable objects will be ignored.
a clindoc object, inherited from an officer::rdocx object
ct <- clintable(mtcars) clindoc(ct)ct <- clintable(mtcars) clindoc(ct)
These functions apply default styling to clintable objects used for
clinical tables, including titles and footnotes. The styling includes
removing borders, setting font properties, and adjusting table width,
line spacing, and padding.
clinify_titles_default(x, ...) clinify_footnotes_default(x, ...) clinify_table_default(x, ...) clinify_caption_default(x, ...) clinify_grouplabel_default(x, ...) clinify_docx_default()clinify_titles_default(x, ...) clinify_footnotes_default(x, ...) clinify_table_default(x, ...) clinify_caption_default(x, ...) clinify_grouplabel_default(x, ...) clinify_docx_default()
x |
A |
... |
Additional arguments (currently unused). |
A clintable object with the applied styling.
op <- options() sect <- clinify_docx_default() # Save out options to grab defaults options( clinify_docx_default = sect, clinify_titles_default = clinify_titles_default, clinify_footnotes_default = clinify_footnotes_default, clinify_table_default = clinify_table_default, clinify_caption_default = clinify_caption_default, clinify_grouplabel_default = clinify_grouplabel_default ) options(op)op <- options() sect <- clinify_docx_default() # Save out options to grab defaults options( clinify_docx_default = sect, clinify_titles_default = clinify_titles_default, clinify_footnotes_default = clinify_footnotes_default, clinify_table_default = clinify_table_default, clinify_caption_default = clinify_caption_default, clinify_grouplabel_default = clinify_grouplabel_default ) options(op)
A clintable object directly inherits from a flextable object. This function
will pass all necessary parameters flextable::flextable() and conver the
object to a clintable
clintable(x, page_by = NULL, group_by = NULL, use_labels = TRUE, ...)clintable(x, page_by = NULL, group_by = NULL, use_labels = TRUE, ...)
x |
A data frame |
page_by |
A variable in the input dataframe to use for pagination |
group_by |
A character vector of variable names which will be used for grouping and attached as a label above the table headers |
use_labels |
Use variable labels as column headers. Nested levels can be achieved using the string "||" as a delimitter. Horizontal and vertical levels using identical words will be merged. |
... |
Parameters to pass to |
A clintable object
clintable(mtcars)clintable(mtcars)
Assigns sequential page numbers to elements of a vector, grouping by unique values and allocating a specified number of rows per page. The input vector must be presorted by group.
make_grouped_pagenums(var, rows)make_grouped_pagenums(var, rows)
var |
A vector of group labels, presorted so that identical values are contiguous. |
rows |
Integer. The maximum number of rows per page. |
The function splits the input vector into groups, then assigns page numbers within each group so that each page contains up to 'rows“ items. Page numbers increment sequentially across groups. If the input is not presorted by group, the function will throw an error.
An integer vector of the same length as 'var“, indicating the assigned page number for each element.
library(dplyr) iris |> mutate( page = make_grouped_pagenums(Species, 5) )library(dplyr) iris |> mutate( page = make_grouped_pagenums(Species, 5) )
Create a new title or footnote flextable
new_title_footnote(x, sect = c("titles", "footnotes", "footnote_page"))new_title_footnote(x, sect = c("titles", "footnotes", "footnote_page"))
x |
a list of character vectors, no more than 3 elements to a vector. |
sect |
Either "titles" or "footnotes" |
A flextable object
title <- new_title_footnote( list( # We'll add tools to automate paging c("Protocol: CDISCPILOT01", "Page {PAGE} of {NUMPAGES}"), c("Table 14-2.01"), c("Summary of Demographic and Baseline Characteristics") ), "titles" ) footnote <- new_title_footnote( list( # We'll add tools to automate paging c("Page {PAGE}", "Total Pages: {NUMPAGES}") ), "footnotes" )title <- new_title_footnote( list( # We'll add tools to automate paging c("Protocol: CDISCPILOT01", "Page {PAGE} of {NUMPAGES}"), c("Table 14-2.01"), c("Summary of Demographic and Baseline Characteristics") ), "titles" ) footnote <- new_title_footnote( list( # We'll add tools to automate paging c("Page {PAGE}", "Total Pages: {NUMPAGES}") ), "footnotes" )
Extraction of flextable print method with special handling of clintable pages and
## S3 method for class 'clintable' print(x, n = 3, nrows = 15, apply_defaults = TRUE, ...) ## S3 method for class 'clintable' knit_print(x, n = 3, nrows = 15, apply_defaults = TRUE, ...)## S3 method for class 'clintable' print(x, n = 3, nrows = 15, apply_defaults = TRUE, ...) ## S3 method for class 'clintable' knit_print(x, n = 3, nrows = 15, apply_defaults = TRUE, ...)
x |
A clintable object |
n |
Number of pages within the clintable to print. Only used when pagination is configured |
nrows |
Number of rows to print. Only used when rows aren't configured within the pagination method |
apply_defaults |
Apply default styles. These styles are stored in the options clinify_header_default, clinify_footer_default, and clinify_table_default respectively. Defaults to true. |
... |
Additional parameters passed to flextable print method |
Invisible
ct <- clintable(mtcars) print(ct) ct <- clin_alt_pages( ct, key_cols = c("mpg", "cyl", "hp"), col_groups = list( c("disp", "drat", "wt"), c("qsec", "vs", "am"), c("gear", "carb") ) ) print(ct)ct <- clintable(mtcars) print(ct) ct <- clin_alt_pages( ct, key_cols = c("mpg", "cyl", "hp"), col_groups = list( c("disp", "drat", "wt"), c("qsec", "vs", "am"), c("gear", "carb") ) ) print(ct)
Write a clinify table out to a docx file
write_clindoc(x, file)write_clindoc(x, file)
x |
a clintable object |
file |
The file path to which the file should be written |
Invisible
ct <- clintable(mtcars) ct <- clin_alt_pages( ct, key_cols = c("mpg", "cyl", "hp"), col_groups = list( c("disp", "drat", "wt"), c("qsec", "vs", "am"), c("gear", "carb") ) ) # Get document object directly doc <- clindoc(ct) # Write out docx file write_clindoc(ct, file.path(tempdir(), "demo.docx"))ct <- clintable(mtcars) ct <- clin_alt_pages( ct, key_cols = c("mpg", "cyl", "hp"), col_groups = list( c("disp", "drat", "wt"), c("qsec", "vs", "am"), c("gear", "carb") ) ) # Get document object directly doc <- clindoc(ct) # Write out docx file write_clindoc(ct, file.path(tempdir(), "demo.docx"))