Main Functions
These functions are built on top of the format
functions, providing a quick and straightforward way to format
tables.
visTable()
visTable()
is a flexible function designed to format any
<data.frame>
.
Let’s demonstrate its usage with a dataset from the
palmerpenguins
package.
library(visOmopResults)
library(palmerpenguins)
library(dplyr)
library(tidyr)
x <- penguins |>
filter(!is.na(sex) & year == 2008) |>
select(!"body_mass_g") |>
summarise(across(ends_with("mm"), ~mean(.x)), .by = c("species", "island", "sex"))
head(x)
#> # A tibble: 6 × 6
#> species island sex bill_length_mm bill_depth_mm flipper_length_mm
#> <fct> <fct> <fct> <dbl> <dbl> <dbl>
#> 1 Adelie Biscoe female 36.6 17.2 187.
#> 2 Adelie Biscoe male 40.8 19.0 193.
#> 3 Adelie Torgersen female 36.6 17.4 190
#> 4 Adelie Torgersen male 40.9 18.8 194.
#> 5 Adelie Dream female 36.3 17.8 189
#> 6 Adelie Dream male 40.1 18.9 195
We can format this data into a <gt>
table using
visTable()
as follows:
visTable(
result = x,
groupColumn = c("sex"),
rename = c("Bill length (mm)" = "bill_length_mm",
"Bill depth (mm)" = "bill_depth_mm",
"Flipper length (mm)" = "flipper_length_mm",
"Body mass (g)" = "body_mass_g"),
type = "gt",
hide = "year"
)
Species | Island | Bill length (mm) | Bill depth (mm) | Flipper length (mm) |
---|---|---|---|---|
female | ||||
Adelie | Biscoe | 36.6444444444444 | 17.2222222222222 | 186.555555555556 |
Torgersen | 36.6125 | 17.4 | 190 | |
Dream | 36.275 | 17.7875 | 189 | |
Gentoo | Biscoe | 45.2954545454545 | 14.1318181818182 | 213 |
Chinstrap | Dream | 46 | 17.3 | 192.666666666667 |
male | ||||
Adelie | Biscoe | 40.7555555555556 | 19.0333333333333 | 192.555555555556 |
Torgersen | 40.925 | 18.8375 | 193.5 | |
Dream | 40.1125 | 18.8875 | 195 | |
Gentoo | Biscoe | 48.5391304347826 | 15.704347826087 | 222.086956521739 |
Chinstrap | Dream | 51.4 | 19.6 | 202.777777777778 |
To use the arguments estimateName
and
header
, the <data.frame>
must have the
estimates arranged into three columns: estimate_name
,
estimate_type
, and estimate_value
. Let’s
reshape the example dataset accordingly and demonstrate creating a
<flextable>
object:
# Transforming the dataset to include estimate columns
x <- x |>
pivot_longer(
cols = ends_with("_mm"),
names_to = "estimate_name",
values_to = "estimate_value"
) |>
mutate(estimate_type = "numeric")
# Creating a formatted flextable
visTable(
result = x,
estimateName = c(
"Bill length (mm)" = "<bill_length_mm>",
"Bill depth (mm)" = "<bill_depth_mm>",
"Flipper length (mm)" = "<flipper_length_mm>"
),
header = c("species", "island"),
groupColumn = "sex",
type = "flextable",
hide = c("year", "estimate_type")
)
Sex | Estimate name | Species | ||||
---|---|---|---|---|---|---|
Adelie | Gentoo | Chinstrap | ||||
Island | ||||||
Biscoe | Torgersen | Dream | Biscoe | Dream | ||
female | ||||||
Bill length (mm) | 36.64 | 36.61 | 36.27 | 45.30 | 46.00 | |
Bill depth (mm) | 17.22 | 17.40 | 17.79 | 14.13 | 17.30 | |
Flipper length (mm) | 186.56 | 190.00 | 189.00 | 213.00 | 192.67 | |
male | ||||||
Bill length (mm) | 40.76 | 40.92 | 40.11 | 48.54 | 51.40 | |
Bill depth (mm) | 19.03 | 18.84 | 18.89 | 15.70 | 19.60 | |
Flipper length (mm) | 192.56 | 193.50 | 195.00 | 222.09 | 202.78 |
visOmopTable()
visOmopTable()
extends the functionality of
visTable()
with additional features tailored specifically
for handling <summarised_result>
objects, making it
easier to work with standardized result formats.
Let’s demonstrate visOmopTable()
with a mock
<summarised_result>
:
# Creating a mock summarised result
result <- mockSummarisedResult() |>
filter(strata_name == "age_group &&& sex")
# Displaying the first few rows
head(result)
#> # A tibble: 6 × 13
#> result_id cdm_name group_name group_level strata_name strata_level
#> <int> <chr> <chr> <chr> <chr> <chr>
#> 1 1 mock cohort_name cohort1 age_group &&& sex <40 &&& Male
#> 2 1 mock cohort_name cohort1 age_group &&& sex >=40 &&& Male
#> 3 1 mock cohort_name cohort1 age_group &&& sex <40 &&& Female
#> 4 1 mock cohort_name cohort1 age_group &&& sex >=40 &&& Female
#> 5 1 mock cohort_name cohort2 age_group &&& sex <40 &&& Male
#> 6 1 mock cohort_name cohort2 age_group &&& sex >=40 &&& Male
#> # ℹ 7 more variables: variable_name <chr>, variable_level <chr>,
#> # estimate_name <chr>, estimate_type <chr>, estimate_value <chr>,
#> # additional_name <chr>, additional_level <chr>
# Creating a formatted gt table
visOmopTable(
result = result,
estimateName = c(
"N%" = "<count> (<percentage>)",
"N" = "<count>",
"Mean (SD)" = "<mean> (<sd>)"
),
header = c("package_name", "age_group"),
groupColumn = c("cohort_name", "sex"),
settingsColumn = "package_name",
type = "gt"
)
Package name
|
|||||
---|---|---|---|---|---|
visOmopResults
|
|||||
CDM name | Variable name | Variable level | Estimate name |
Age group
|
|
<40 | >=40 | ||||
cohort1; Male | |||||
mock | number subjects | - | N | 126,159 | 4,731,729 |
age | - | Mean (SD) | 23.58 (1.32) | 54.27 (7.46) | |
Medications | Amoxiciline | N% | 42,735 (70.16) | 33,512 (47.85) | |
Ibuprofen | N% | 35,734 (83.09) | 95,213 (63.61) | ||
cohort1; Female | |||||
mock | number subjects | - | N | 884,415 | 8,347,724 |
age | - | Mean (SD) | 25.07 (0.56) | 34.51 (8.77) | |
Medications | Amoxiciline | N% | 26,061 (49.13) | 45,164 (14.38) | |
Ibuprofen | N% | 70,395 (86.33) | 99,230 (56.60) | ||
cohort2; Male | |||||
mock | number subjects | - | N | 4,830,852 | 3,522,444 |
age | - | Mean (SD) | 16.06 (0.47) | 51.08 (2.97) | |
Medications | Amoxiciline | N% | 12,440 (4.30) | 44,111 (62.16) | |
Ibuprofen | N% | 82,589 (29.75) | 68,208 (90.24) | ||
cohort2; Female | |||||
mock | number subjects | - | N | 5,944,911 | 2,271,551 |
age | - | Mean (SD) | 1.13 (3.19) | 33.17 (8.59) | |
Medications | Amoxiciline | N% | 67,582 (98.93) | 31,016 (48.29) | |
Ibuprofen | N% | 33,537 (1.12) | 5,466 (48.09) |
The workflow is quite similar to visTable()
, but it
includes specific enhancements for
<summarised_result>
objects:
Automatic splitting: The result object is always processed using the
splitAll()
function. Thereby, column names to use in other arguments must be based on the split result.settingsColumn
argument: Use this argument to specify which settings should be displayed in the main table. The columns specified here can also be referenced in other arguments such asheader
,rename
, andgroupColumn.
header
argument: accepts specific<summarised_result>
inputs, in addition to its typical usage as invisTable()
. For example, use “strata” in the header to display all variables instrata_name
, or use “settings” to show all settings specified insettingsColumns.
Hidden columns: result_id and estimate_type columns are always hidden as they serve as helper columns for internal processes.
Suppressed estimates: if the result object has been processed with suppress(), obscured estimates can be displayed as the default
na
value or as “<{minCellCount}” with the corresponding minimum count value used. This can be controlled using theshowMinCellCount
argument.
In the next example, visOmopTable()
generates a
<gt>
table while displaying suppressed estimates
(those with counts below 1,000,000) with the specified minimum cell
count.
result |>
suppress(minCellCount = 1000000) |>
visOmopTable(
estimateName = c(
"N%" = "<count> (<percentage>)",
"N" = "<count>",
"Mean (SD)" = "<mean> (<sd>)"
),
header = c("My visOmopTable", "group"),
groupColumn = c("strata"),
hide = c("cdm_name"),
showMinCellCount = TRUE,
type = "gt"
)
My visOmopTable
|
||||
---|---|---|---|---|
Variable name | Variable level | Estimate name |
Cohort name
|
|
cohort1 | cohort2 | |||
<40; Male | ||||
number subjects | - | N | <1,000,000 | 4,830,852 |
age | - | Mean (SD) | <1,000,000 (<1,000,000) | 16.06 (0.47) |
Medications | Amoxiciline | N% | <1,000,000 (<1,000,000) | <1,000,000 (<1,000,000) |
Ibuprofen | N% | <1,000,000 (<1,000,000) | <1,000,000 (<1,000,000) | |
>=40; Male | ||||
number subjects | - | N | 4,731,729 | 3,522,444 |
age | - | Mean (SD) | 54.27 (7.46) | 51.08 (2.97) |
Medications | Amoxiciline | N% | <1,000,000 (<1,000,000) | <1,000,000 (<1,000,000) |
Ibuprofen | N% | <1,000,000 (<1,000,000) | <1,000,000 (<1,000,000) | |
<40; Female | ||||
number subjects | - | N | <1,000,000 | 5,944,911 |
age | - | Mean (SD) | <1,000,000 (<1,000,000) | 1.13 (3.19) |
Medications | Amoxiciline | N% | <1,000,000 (<1,000,000) | <1,000,000 (<1,000,000) |
Ibuprofen | N% | <1,000,000 (<1,000,000) | <1,000,000 (<1,000,000) | |
>=40; Female | ||||
number subjects | - | N | 8,347,724 | 2,271,551 |
age | - | Mean (SD) | 34.51 (8.77) | 33.17 (8.59) |
Medications | Amoxiciline | N% | <1,000,000 (<1,000,000) | <1,000,000 (<1,000,000) |
Ibuprofen | N% | <1,000,000 (<1,000,000) | <1,000,000 (<1,000,000) |
Styling tables
Tables displayed in visOmopResults()
follow a default
style, but customization is possible through the .options
argument. This argument allows users to modify various formatting
aspects using options from the format
functions (see the
format
Functions section to learn more).
The table below details which format
function each
styling option belongs to, along with a description of each option:
Argument | Description |
---|---|
formatEstimateValue() | |
decimals | Number of decimals per estimate type (integer, numeric, percentage, proportion), estimate name, or all estimate values (introduce the number of decimals). |
decimalMark | Decimal separator mark. |
bigMark | Thousand and millions separator mark. |
formatEstimateName() | |
keepNotFormatted | Whether to keep rows not formatted. |
useFormatOrder | Whether to use the order in which estimate names appear in the estimateName (TRUE), or use the order in the input dataframe (FALSE). |
formatHeader() | |
delim | Delimiter to use to separate headers. |
includeHeaderName | Whether to include the column name as header. |
includeHeaderKey | Whether to include the header key (header, header_name, header_level) before each header type in the column names. |
formatTable() | |
style | Named list that specifies how to style the different parts of the gt or flextable table generated. Accepted style entries are: title, subtitle, header, header_name, header_level, column_name, group_label, and body. Alternatively, use 'default' to get visOmopResults style, or NULL for gt/flextable style. Keep in mind that styling code is different for gt and flextable. To see the 'deafult' gt style code use gtStyle(), and flextableStyle() for flextable default code style |
na | How to display missing values. |
title | Title of the table, or NULL for no title. |
subtitle | Subtitle of the table, or NULL for no subtitle. |
caption | Caption for the table, or NULL for no caption. Text in markdown formatting style (e.g. *Your caption here* for caption in italics) |
groupAsColumn | Whether to display the group labels as a column (TRUE) or rows (FALSE). |
groupOrder | Order in which to display group labels. |
merge | Names of the columns to merge vertically when consecutive row cells have identical values. Alternatively, use 'all_columns' to apply this merging to all columns, or use NULL to indicate no merging. |
To view the default .options
settings used in
vis
tables, use the following function:
tableOptions()
#> $decimals
#> integer percentage numeric proportion
#> 0 2 2 2
#>
#> $decimalMark
#> [1] "."
#>
#> $bigMark
#> [1] ","
#>
#> $keepNotFormatted
#> [1] TRUE
#>
#> $useFormatOrder
#> [1] TRUE
#>
#> $delim
#> [1] "\n"
#>
#> $includeHeaderName
#> [1] TRUE
#>
#> $includeHeaderKey
#> [1] TRUE
#>
#> $style
#> [1] "default"
#>
#> $na
#> [1] "-"
#>
#> $title
#> NULL
#>
#> $subtitle
#> NULL
#>
#> $caption
#> NULL
#>
#> $groupAsColumn
#> [1] FALSE
#>
#> $groupOrder
#> NULL
#>
#> $merge
#> [1] "all_columns"
Styling <gt>
and
<flextable>
To inspect the code for the default styles of <gt>
and <flextable>
, use these functions:
tableStyle(type = "gt")
#> list(header = list(gt::cell_fill(color = "#c8c8c8"), gt::cell_text(weight = "bold",
#> align = "center")), header_name = list(gt::cell_fill(color = "#d9d9d9"),
#> gt::cell_text(weight = "bold", align = "center")), header_level = list(gt::cell_fill(color = "#e1e1e1"),
#> gt::cell_text(weight = "bold", align = "center")), column_name = list(gt::cell_text(weight = "bold",
#> align = "center")), group_label = list(gt::cell_fill(color = "#e9e9e9"),
#> gt::cell_text(weight = "bold")), title = list(gt::cell_text(weight = "bold",
#> size = 15, align = "center")), subtitle = list(gt::cell_text(weight = "bold",
#> size = 12, align = "center")), body = list())
tableStyle(type = "flextable")
#> list(header = list(cell = officer::fp_cell(background.color = "#c8c8c8"),
#> text = officer::fp_text(bold = TRUE)), header_name = list(cell = officer::fp_cell(background.color = "#d9d9d9"),
#> text = officer::fp_text(bold = TRUE)), header_level = list(cell = officer::fp_cell(background.color = "#e1e1e1"),
#> text = officer::fp_text(bold = TRUE)), column_name = list(text = officer::fp_text(bold = TRUE)),
#> group_label = list(cell = officer::fp_cell(background.color = "#e9e9e9",
#> border = officer::fp_border(color = "gray")), text = officer::fp_text(bold = TRUE)),
#> title = list(text = officer::fp_text(bold = TRUE, font.size = 15)),
#> subtitle = list(text = officer::fp_text(bold = TRUE, font.size = 12)),
#> body = list())