| Title: | Create a Table with Row, Column, and Table Annotations |
|---|---|
| Description: | Offers a TableContainer() function to create tables enriched with row, column, and table annotations. This package is similar to 'SummarizedExperiment' in Bioconductor <doi:10.18129/B9.bioc.SummarizedExperiment>, but designed to work independently of Bioconductor, it ensures annotations are automatically updated when the table is subset. Additionally, it includes format_tbl() methods for enhanced table formatting and display. |
| Authors: | Jiefei Wang [aut, cre] (ORCID: <https://orcid.org/0000-0002-2709-5332>) |
| Maintainer: | Jiefei Wang <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-05-19 08:43:22 UTC |
| Source: | https://github.com/jiefei-wang/tablecontainer |
The show method uses .printTable to display the table and .printMeta to display the rowMeta, colMeta, and metaData.
.printTable(tbl, ...) .printMeta(meta, name) ## S4 method for signature 'TableContainer' show(object).printTable(tbl, ...) .printMeta(meta, name) ## S4 method for signature 'TableContainer' show(object)
tbl |
A table-like object (e.g., matrix, data.frame). |
... |
Additioanl arguments passed to the |
meta |
A list of metadata items. |
name |
A string representing the name of the metadata. |
object |
A TableContainer object. |
.printTable: No return value, called for side effects of printing the table to the console.
.printMeta: No return value, called for side effects of printing metadata information to the console.
show: No return value, called for side effects of displaying the TableContainer object contents to the console.
Data Conversion
## S4 method for signature 'TableContainer' as.matrix(x) ## S4 method for signature 'TableContainer' as.data.frame(x, row.names = NULL, optional = FALSE, ...)## S4 method for signature 'TableContainer' as.matrix(x) ## S4 method for signature 'TableContainer' as.data.frame(x, row.names = NULL, optional = FALSE, ...)
x |
A TableContainer object. |
row.names |
|
optional |
Logical. If |
... |
additional arguments |
as.matrix: A matrix representation of the TableContainer object.
as.data.frame: A data frame representation of the TableContainer object.
Container Methods
## S4 method for signature 'TableContainer' dim(x) ## S4 method for signature 'TableContainer' dimnames(x) ## S4 method for signature 'TableContainer' nrow(x) ## S4 method for signature 'TableContainer' ncol(x) ## S4 method for signature 'TableContainer' rownames(x) ## S4 replacement method for signature 'TableContainer' rownames(x) <- value ## S4 method for signature 'TableContainer' colnames(x) ## S4 replacement method for signature 'TableContainer' colnames(x) <- value ## S4 method for signature 'TableContainer' x[i, j, ..., drop = TRUE] tblData(object) tblData(object) <- value rowData(object) rowData(object) <- value colData(object) colData(object) <- value metaData(object) metaData(object) <- value ## S4 method for signature 'TableContainer' tblData(object) ## S4 replacement method for signature 'TableContainer' tblData(object) <- value ## S4 method for signature 'TableContainer' rowData(object) ## S4 replacement method for signature 'TableContainer' rowData(object) <- value ## S4 method for signature 'TableContainer' colData(object) ## S4 replacement method for signature 'TableContainer' colData(object) <- value ## S4 method for signature 'TableContainer' metaData(object) ## S4 replacement method for signature 'TableContainer' metaData(object) <- value## S4 method for signature 'TableContainer' dim(x) ## S4 method for signature 'TableContainer' dimnames(x) ## S4 method for signature 'TableContainer' nrow(x) ## S4 method for signature 'TableContainer' ncol(x) ## S4 method for signature 'TableContainer' rownames(x) ## S4 replacement method for signature 'TableContainer' rownames(x) <- value ## S4 method for signature 'TableContainer' colnames(x) ## S4 replacement method for signature 'TableContainer' colnames(x) <- value ## S4 method for signature 'TableContainer' x[i, j, ..., drop = TRUE] tblData(object) tblData(object) <- value rowData(object) rowData(object) <- value colData(object) colData(object) <- value metaData(object) metaData(object) <- value ## S4 method for signature 'TableContainer' tblData(object) ## S4 replacement method for signature 'TableContainer' tblData(object) <- value ## S4 method for signature 'TableContainer' rowData(object) ## S4 replacement method for signature 'TableContainer' rowData(object) <- value ## S4 method for signature 'TableContainer' colData(object) ## S4 replacement method for signature 'TableContainer' colData(object) <- value ## S4 method for signature 'TableContainer' metaData(object) ## S4 replacement method for signature 'TableContainer' metaData(object) <- value
x |
A TableContainer object. |
value |
A matrix, data.frame, or NULL. |
i |
Row indices for subsetting. If only |
j |
Column indices for subsetting. |
... |
Additional arguments. |
drop |
Not used. |
object |
A TableContainer object. |
dim, dimnames, nrow, and ncol: the respective dimensions, dimnames, number of rows, and number of columns of the TableContainer object. When the table slot is NULL, the dimensions are derived from the rowData and colData slots.
[: A new TableContainer object with the selected data.
tblData, rowData, colData, and metadata: the respective slots of the TableContainer object.
tblData<-, rowData<-, colData<-, and metadata<-: update the respective slots and return the modified object.
Format a table for display. The maximum number of columns displayed is limited by the terminal width.
format_tbl( tbl, max_tbl_width, soft_table_width = max_tbl_width, max_row = 8, cell_formatter = common_formatter, delimiter = " ", truncate_marker = " ...", header_name = "", leading_text = NULL, include_row_names = TRUE, include_col_names = TRUE )format_tbl( tbl, max_tbl_width, soft_table_width = max_tbl_width, max_row = 8, cell_formatter = common_formatter, delimiter = " ", truncate_marker = " ...", header_name = "", leading_text = NULL, include_row_names = TRUE, include_col_names = TRUE )
tbl |
A table-like object (e.g., matrix, data.frame). |
max_tbl_width |
The maximum width for the table display. |
soft_table_width |
A softer width limit for the table display, allowing the last column to exceed it. |
max_row |
The maximum number of rows to display. |
cell_formatter |
A function to format individual cells. |
delimiter |
The delimiter to use for separating columns. |
truncate_marker |
A marker to indicate truncation of the table. |
header_name |
A string to print as the first element in the column names row. It only works when |
leading_text |
A string to print before the table. This can be used to indent the entire table. It can be a single string or a vector of strings. If it is a vector, it will be recycled to match the number of rows in the table. |
include_row_names |
Logical, whether to include row names in the display. |
include_col_names |
Logical, whether to include column names in the display. |
A formatted string vector representing the table. The length of the vector is the number of rows in the table.
common_formatter() for the cell formatting function and pretty_number() for formatting numeric values.
## Format the table tbl <- data.frame( x = c(1, 123, 123456678, 1235678887644), y = c("abc", "this is a long string", "another long string", "yet another long string"), z = c(TRUE, FALSE, TRUE, FALSE), d = runif(4) * 100000 ) formatted_tbl <- format_tbl(tbl, max_tbl_width = 50, max_row = 3) cat(formatted_tbl, sep = "\n")## Format the table tbl <- data.frame( x = c(1, 123, 123456678, 1235678887644), y = c("abc", "this is a long string", "another long string", "yet another long string"), z = c(TRUE, FALSE, TRUE, FALSE), d = runif(4) * 100000 ) formatted_tbl <- format_tbl(tbl, max_tbl_width = 50, max_row = 3) cat(formatted_tbl, sep = "\n")
Common functions for formatting table cells
Common functions for formatting table cells
pretty_number(x, max_len = 20) common_formatter(x, max_len = 20)pretty_number(x, max_len = 20) common_formatter(x, max_len = 20)
x |
A vector of values to format. |
max_len |
The maximum length for the formatted string. |
pretty_number : keep all digits if we can keep it within the width limit. Otherwise, use scientific notation to reduce length. If the number still cannot fit within the length limit, return the shortest result.
common_formatter : For numeric, call pretty_number to format the number. For non-numeric, truncate the string and append "..." if it exceeds the width limit.
A character string with the formatted number. If the number cannot fit within the specified maximum length, scientific notation is used with reduced digits.
A character vector of the same length as x with formatted values. For numeric values, uses pretty_number() formatting. For non-numeric values, truncates strings longer than max_len and appends "..." if needed.
format_tbl() for formatting tables
## Format a number pretty_number(1234567890, max_len = 20) pretty_number(1234567890, max_len = 8) pretty_number(1234567890, max_len = 3) ## format character common_formatter("this is a long string", max_len = 40) common_formatter("this is a long string", max_len = 20)## Format a number pretty_number(1234567890, max_len = 20) pretty_number(1234567890, max_len = 8) pretty_number(1234567890, max_len = 3) ## format character common_formatter("this is a long string", max_len = 40) common_formatter("this is a long string", max_len = 20)
Creates a TableContainer object with the specified matrix, rowData, colData, and metadata.
TableContainer(table = NULL, rowData = NULL, colData = NULL, metaData = NULL)TableContainer(table = NULL, rowData = NULL, colData = NULL, metaData = NULL)
table |
A matrix, data.frame, or NULL. |
rowData |
A data.frame or NULL object describing the rows. |
colData |
A data.frame or NULL object describing the columns. |
metaData |
A list or NULL containing arbitrary metadata associated with the overall data. |
A TableContainer object.
tbl <- matrix(1:12, nrow = 3, ncol = 4) row_dt <- data.frame(row1 = 1:3, row2 = letters[1:3]) col_dt <- data.frame(col1 = 1:4, col2 = letters[1:4]) meta_dt <- list(meta1 = "meta1", meta2 = "meta2") TableContainer( table = tbl, rowData = row_dt, colData = col_dt, metaData = meta_dt )tbl <- matrix(1:12, nrow = 3, ncol = 4) row_dt <- data.frame(row1 = 1:3, row2 = letters[1:3]) col_dt <- data.frame(col1 = 1:4, col2 = letters[1:4]) meta_dt <- list(meta1 = "meta1", meta2 = "meta2") TableContainer( table = tbl, rowData = row_dt, colData = col_dt, metaData = meta_dt )
A container for a matrix and associated row/column annotations. This is similar in concept to Bioconductor's SummarizedExperiment but with no Bioconductor dependencies.
tableA matrix, data.frame, or NULL.
rowDataA data.frame or NULL object describing the rows. Each row of the matrix corresponds to a row in the rowData object.
colDataA data.frame or NULL object describing the columns. Each column of the matrix corresponds to a row in the colData object.
metaDataA list or NULL containing arbitrary metadata associated with the overall data.