Package 'mmtable2'

Title: Create and combine tables with a ggplot2/patchwork syntax
Description: Add headers using data from a column in your data frame. Combine tables with +, / and * operators. Output tables in gt package format.
Authors: Ian Moran
Maintainer: Ian Moran <[email protected]>
License: MIT + file LICENSE
Version: 0.1.3
Built: 2024-10-21 03:33:55 UTC
Source: https://github.com/ianmoran11/mmtable2

Help Index


Integrate two tables horizontally

Description

Integrate two tables horizontally

Usage

## S3 method for class 'mmtable'
mmtable1 * mmtable2

Arguments

mmtable1

an mmtable

mmtable2

an mmtable

Value

an mmtable


Concatenate tables horizontally

Description

Concatenate tables horizontally

Usage

## S3 method for class 'mmtable'
e1 / e2

Arguments

e1

an mmtable

e2

an mmtable

Value

an mmtable


Integrate two tables horizontally

Description

Integrate two tables horizontally

Usage

## S3 method for class 'mmtable'
mmtable1 + mmtable2

Arguments

mmtable1

an mmtable

mmtable2

an mmtable

Value

an mmtable


Add cells formatting

Description

Add cells formatting

Usage

add_cells_format(mmtable, ...)

Arguments

mmtable

an mmtable object

...

arguments for add_cells_format

Value

an mmtable

Examples

## Not run: 

library(tidyverse)

gm_table_piped <-
  gapminder_mm %>%
  filter(var != "Life expectancy") %>%
  mmtable(cells = value, use_defaul_formats = T) %>%
  add_header_top(year) %>%
  add_header_left(country) %>%
  add_header_top_left(var)  %>%
  add_header_left_top(continent)  %>%
  add_cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) %>%
  add_header_format(header = year, style = list(cell_text(align = "right"))) %>%
  add_header_format("all_cols", style = list(cell_text(weight = "bolder"))) %>%
  add_header_format("all_rows", style = list(cell_text(weight = "bolder"))) %>%
  add_table_format(
    locations = cells_body(rows = c(1,3,5,7,9,11)),
    style = list(cell_borders(sides = "top",color = "grey"))) %>%
  add_table_source_note(source_note = "Excerpt of the Gapminder dataset." )

gm_table_piped

## End(Not run)

Add formatting to a header

Description

Add formatting to a header

Usage

add_header_format(mmtable, ...)

Arguments

mmtable

an mmtable object

...

arguments for add_header_format

Value

an mmtable

Examples

## Not run: 

library(tidyverse)

gm_table_piped <-
  gapminder_mm %>%
  filter(var != "Life expectancy") %>%
  mmtable(cells = value, use_defaul_formats = T) %>%
  add_header_top(year) %>%
  add_header_left(country) %>%
  add_header_top_left(var)  %>%
  add_header_left_top(continent)  %>%
  add_cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) %>%
  add_header_format(header = year, style = list(cell_text(align = "right"))) %>%
  add_header_format("all_cols", style = list(cell_text(weight = "bolder"))) %>%
  add_header_format("all_rows", style = list(cell_text(weight = "bolder"))) %>%
  add_table_format(
    locations = cells_body(rows = c(1,3,5,7,9,11)),
    style = list(cell_borders(sides = "top",color = "grey"))) %>%
  add_table_source_note(source_note = "Excerpt of the Gapminder dataset." )

gm_table_piped

## End(Not run)

Add a left header

Description

Add a left header

Usage

add_header_left(mmtable, ...)

Arguments

mmtable

an mmtable object

...

arguments for header_left

Value

an mmtable object

Examples

## Not run: 

library(tidyverse)

gm_table_piped <-
  gapminder_mm %>%
  filter(var != "Life expectancy") %>%
  mmtable(cells = value, use_defaul_formats = T) %>%
  add_header_top(year) %>%
  add_header_left(country) %>%
  add_header_top_left(var)  %>%
  add_header_left_top(continent)  %>%
  add_cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) %>%
  add_header_format(header = year, style = list(cell_text(align = "right"))) %>%
  add_header_format("all_cols", style = list(cell_text(weight = "bolder"))) %>%
  add_header_format("all_rows", style = list(cell_text(weight = "bolder"))) %>%
  add_table_format(
    locations = cells_body(rows = c(1,3,5,7,9,11)),
    style = list(cell_borders(sides = "top",color = "grey"))) %>%
  add_table_source_note(source_note = "Excerpt of the Gapminder dataset." )

gm_table_piped

## End(Not run)

Add a left top header

Description

Add a left top header

Usage

add_header_left_top(mmtable, ...)

Arguments

mmtable

an mmtable object

...

arguments for header_left_top

Value

an mmtable object

Examples

## Not run: 

library(tidyverse)

gm_table_piped <-
  gapminder_mm %>%
  filter(var != "Life expectancy") %>%
  mmtable(cells = value, use_defaul_formats = T) %>%
  add_header_top(year) %>%
  add_header_left(country) %>%
  add_header_top_left(var)  %>%
  add_header_left_top(continent)  %>%
  add_cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) %>%
  add_header_format(header = year, style = list(cell_text(align = "right"))) %>%
  add_header_format("all_cols", style = list(cell_text(weight = "bolder"))) %>%
  add_header_format("all_rows", style = list(cell_text(weight = "bolder"))) %>%
  add_table_format(
    locations = cells_body(rows = c(1,3,5,7,9,11)),
    style = list(cell_borders(sides = "top",color = "grey"))) %>%
  add_table_source_note(source_note = "Excerpt of the Gapminder dataset." )

gm_table_piped

## End(Not run)

Add a top header

Description

Add a top header

Usage

add_header_top(mmtable, ...)

Arguments

mmtable

an mmtable object

...

arguments for header_top

Value

an mmtable object

Examples

## Not run: 

library(tidyverse)

gm_table_piped <-
  gapminder_mm %>%
  filter(var != "Life expectancy") %>%
  mmtable(cells = value, use_defaul_formats = T) %>%
  add_header_top(year) %>%
  add_header_left(country) %>%
  add_header_top_left(var)  %>%
  add_header_left_top(continent)  %>%
  add_cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) %>%
  add_header_format(header = year, style = list(cell_text(align = "right"))) %>%
  add_header_format("all_cols", style = list(cell_text(weight = "bolder"))) %>%
  add_header_format("all_rows", style = list(cell_text(weight = "bolder"))) %>%
  add_table_format(
    locations = cells_body(rows = c(1,3,5,7,9,11)),
    style = list(cell_borders(sides = "top",color = "grey"))) %>%
  add_table_source_note(source_note = "Excerpt of the Gapminder dataset." )

gm_table_piped

## End(Not run)

Add a top left header

Description

Add a top left header

Usage

add_header_top_left(mmtable, ...)

Arguments

mmtable

an mmtable object

...

arguments for header_top_left

Value

an mmtable object

Examples

## Not run: 

library(tidyverse)

gm_table_piped <-
  gapminder_mm %>%
  filter(var != "Life expectancy") %>%
  mmtable(cells = value, use_defaul_formats = T) %>%
  add_header_top(year) %>%
  add_header_left(country) %>%
  add_header_top_left(var)  %>%
  add_header_left_top(continent)  %>%
  add_cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) %>%
  add_header_format(header = year, style = list(cell_text(align = "right"))) %>%
  add_header_format("all_cols", style = list(cell_text(weight = "bolder"))) %>%
  add_header_format("all_rows", style = list(cell_text(weight = "bolder"))) %>%
  add_table_format(
    locations = cells_body(rows = c(1,3,5,7,9,11)),
    style = list(cell_borders(sides = "top",color = "grey"))) %>%
  add_table_source_note(source_note = "Excerpt of the Gapminder dataset." )

gm_table_piped

## End(Not run)

Add a spanner to a table

Description

Add a spanner to a table

Usage

add_spanner(gm_table2, spanner_list)

Arguments

gm_table2

a mmtable

spanner_list

a list of spannner calls

Value

mmtable


Add formatting to a table

Description

Add formatting to a table

Usage

add_table_format(mmtable, ...)

Arguments

mmtable

an mmtable object

...

arguments for table_format

Value

an mmtable

Examples

## Not run: 

library(tidyverse)

gm_table_piped <-
  gapminder_mm %>%
  filter(var != "Life expectancy") %>%
  mmtable(cells = value, use_defaul_formats = T) %>%
  add_header_top(year) %>%
  add_header_left(country) %>%
  add_header_top_left(var)  %>%
  add_header_left_top(continent)  %>%
  add_cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) %>%
  add_header_format(header = year, style = list(cell_text(align = "right"))) %>%
  add_header_format("all_cols", style = list(cell_text(weight = "bolder"))) %>%
  add_header_format("all_rows", style = list(cell_text(weight = "bolder"))) %>%
  add_table_format(
    locations = cells_body(rows = c(1,3,5,7,9,11)),
    style = list(cell_borders(sides = "top",color = "grey"))) %>%
  add_table_source_note(source_note = "Excerpt of the Gapminder dataset." )

gm_table_piped

## End(Not run)

Add a table source note

Description

Add a table source note

Usage

add_table_source_note(mmtable, ...)

Arguments

mmtable

an mmtable object

...

arguments of gt::tab_source_note

Value

an mmtable


Apply formating to a mmtable

Description

Apply formating to a mmtable

Usage

apply_format(mmtable, format_list)

Arguments

mmtable

an mmtable

format_list

a list of styles of the same format as the gt::tab_style list argument https://gt.rstudio.com/reference/tab_style.html.

Value

mmtable


Apply list of formating commands to a mmtable

Description

Apply list of formating commands to a mmtable

Usage

apply_formats(mmtable)

Arguments

mmtable

an mmtable

Value

mmtable


Conditionally apply a function

Description

Conditionally apply a function

Usage

apply_if(predicate, func, data, ...)

Arguments

predicate

a predicate

func

a function to apply

data

data used by the function or otherwise returned

...

other objects involved in evaluation of the function

Value

data


Format table cells

Description

Format table cells

Usage

cells_format(cell_predicate, ...)

Arguments

cell_predicate

an expression on the source data frame identifying which cells should be formatted.

...

dots.

Value

format list

Examples

## Not run: 
gm_table_formatted <-
  gapminder_mm %>%
  dplyr::filter(var != "Life expectancy") %>%
  mmtable(cells = value) +
  header_top(year) +
  header_left(country) +
  header_top_left(var)  +
  header_left_top(continent)  +
  cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) +
  header_format(header = year, style = list(cell_text(align = "right"))) +
  header_format("all_cols", style = list(cell_text(weight = "bolder"))) +
  header_format("all_rows", style = list(cell_text(weight = "bolder"))) +
  table_format(
    locations = cells_body(rows = c(1,3,5,7,9,11)),
    style = list(cell_borders(sides = "top",color = "grey"))) +
  table_source_note(source_note = "Excerpt of the Gapminder data" )

## End(Not run)

Remove adjacent repeats in a dataframe

Description

Remove adjacent repeats in a dataframe

Usage

diagonalize(df, var_char)

Arguments

df

a data frame

var_char

the column to be diagonalized

Value

data frame


Extract the largest binary rectary from a list of rows and cols

Description

Extract the largest binary rectary from a list of rows and cols

Usage

extract_max_rects(extraction_list)

Arguments

extraction_list

a list containting a dataframe of rows and cols and a list of already extracted rows and cols.

Value

extraction_list


Apply formatting to a location

Description

Apply formatting to a location

Usage

format_a_loc(mmtable, loc, format_list)

Arguments

mmtable

an mmtable

loc

a list containing a row and col items

format_list

a list of formatting commands

Value

format list


Gapminder data.

Description

Excerpt of the Gapminder data on life expectancy, GDP per capita, and population by country.

Usage

gapminder_mm

Format

A data frame with 53940 rows and 10 variables:

continent

price, in US dollars

country

weight of the diamond, in carats

value

weight of the diamond, in carats

var

weight of the diamond, in carats

year

weight of the diamond, in carats

... continent country value var year

Source

http://www.diamondse.info/


Generate a random string

Description

Generate a random string

Usage

gen_random_ids(n, length)

Arguments

n

number of ids

length

length of id

Value

format list


Identify which locations are to be formatted.

Description

Identify which locations are to be formatted.

Usage

get_locations(
  mmtable,
  header = NULL,
  func,
  cell_predicate = NULL,
  scope = "cell"
)

Arguments

mmtable

an mmtable

header

header from original data

func

the original formatting function applied

cell_predicate

predicate to determine locations

scope

string determining how widely formating will apply. Options include "cell", "headers" and "table"

Value

locations


An algorthm to identify the largest rectangle in a histogram

Description

An algorthm to identify the largest rectangle in a histogram

Usage

get_max_area_bf(heights)

Arguments

heights

a list of the heights of the histogram.

Value

list


Get row header names.

Description

Get row header names.

Usage

get_row_header_names(df, col_header_df_01)

Arguments

df

a dataframe

col_header_df_01

a dataframe of col headers

Value

a vector of names


Get the html associated with a spanner

Description

Get the html associated with a spanner

Usage

get_spanner_html_text(table)

Arguments

table

an mmtable

Value

html


Format the cells of a header.

Description

Format the cells of a header.

Usage

header_format(header, ...)

Arguments

header

the name of a variable that is currently a header in the table

...

a list of styles, with format required by gt::tab_style style argument. https://gt.rstudio.com/reference/tab_style.html

Value

format list


Add a left header

Description

Add a left header

Usage

header_left(variable)

Arguments

variable

a column of the data frame from which header values will be constructed

Value

an mmtable object

Examples

## Not run: 

library(tidyverse)

row_list <- cells_body(rows = c(1,3,5,7,9,11))
style_list <- list(cell_borders(sides = "top",color = "grey"))
gm_df <- gapminder_mm %>% dplyr::filter(var != "Life expectancy")

gm_table <-
  gm_df %>%
  mmtable(cells = value) +
  header_top(year) +
  header_left(country) +
  header_top_left(var)  +
  header_left_top(continent)  +
  table_format(
    locations = row_list,
    style = style_list)

## End(Not run)

Add a left top header

Description

Add a left top header

Usage

header_left_top(variable)

Arguments

variable

a column of the data frame from which header values will be constructed

Value

an mmtable object

Examples

## Not run: 

library(tidyverse)

row_list <- cells_body(rows = c(1,3,5,7,9,11))
style_list <- list(cell_borders(sides = "top",color = "grey"))
gm_df <- gapminder_mm %>% dplyr::filter(var != "Life expectancy")

gm_table <-
  gm_df %>%
  mmtable(cells = value) +
  header_top(year) +
  header_left(country) +
  header_top_left(var)  +
  header_left_top(continent)  +
  table_format(
    locations = row_list,
    style = style_list)

## End(Not run)

Merge header columns

Description

Merge header columns

Usage

header_merged_cols(...)

Arguments

...

formating instructions

Value

format list


Add a top header

Description

Add a top header

Usage

header_top(variable)

Arguments

variable

a column of the data frame from which header values will be constructed

Value

an mmtable object

Examples

## Not run: 

library(tidyverse)

row_list <- cells_body(rows = c(1,3,5,7,9,11))
style_list <- list(cell_borders(sides = "top",color = "grey"))
gm_df <- gapminder_mm %>% dplyr::filter(var != "Life expectancy")

gm_table <-
  gm_df %>%
  mmtable(cells = value) +
  header_top(year) +
  header_left(country) +
  header_top_left(var)  +
  header_left_top(continent)  +
  table_format(
    locations = row_list,
    style = style_list)

## End(Not run)

Add a top left header

Description

Add a top left header

Usage

header_top_left(variable)

Arguments

variable

a column of the data frame from which header values will be constructed

Value

an mmtable object

Examples

## Not run: 

library(tidyverse)

row_list <- cells_body(rows = c(1,3,5,7,9,11))
style_list <- list(cell_borders(sides = "top",color = "grey"))
gm_df <- gapminder_mm %>% dplyr::filter(var != "Life expectancy")

gm_table <-
  gm_df %>%
  mmtable(cells = value) +
  header_top(year) +
  header_left(country) +
  header_top_left(var)  +
  header_left_top(continent)  +
  table_format(
    locations = row_list,
    style = style_list)

## End(Not run)

Print an mmtable object

Description

Print an mmtable object

Usage

knit_print.mmtable(x, ...)

Arguments

x

an mmtable

...

other things


Create an mmtable object

Description

Create an mmtable object

Usage

mmtable(
  data,
  cells,
  table_name = NULL,
  use_default_formats = TRUE,
  table_data = NULL
)

Arguments

data

a data frame

cells

the name of the column whose values will form the data cells.

table_name

the name of the table - used for labeling when joined with another table - randomly generated if not provided.

use_default_formats

determines whether or note the table will have default formatting applied. Default value = TRUE.

table_data

deprecated. Use 'cells' instead.

Value

mmtable

Examples

## Not run: 

library(tidyverse)

row_list <- cells_body(rows = c(1,3,5,7,9,11))
style_list <- list(cell_borders(sides = "top",color = "grey"))
gm_df <- gapminder_mm %>% dplyr::filter(var != "Life expectancy")

gm_table <-
  gm_df %>%
  mmtable(cells = value) +
  header_top(year) +
  header_left(country) +
  header_top_left(var)  +
  header_left_top(continent)  +
  table_format(
    locations = row_list,
    style = style_list)

## End(Not run)

Print an mmtable object

Description

Print an mmtable object

Usage

## S3 method for class 'mmtable'
print(x, ...)

Arguments

x

an mmtable

...

other things


Set the class of an object

Description

Set the class of an object

Usage

set_class(object, class)

Arguments

object

a object

class

new class of object

Value

mmtable


Convert a list of cells to a list of cell ranges

Description

Convert a list of cells to a list of cell ranges

Usage

single_cells_to_vector(row_col_list)

Arguments

row_col_list

list of cells


Sort and select columns

Description

Sort and select columns

Usage

sort_and_select_columns(data, ...)

Arguments

data

a data frame

...

list of data to load into envrionment

Value

mmtable


Convert header to spanner

Description

Convert header to spanner

Usage

spannerize(gm_table2, n)

Arguments

gm_table2

a mmtable

n

number of spanners

Value

mmtable


Spread rows to columns

Description

Spread rows to columns

Usage

spread_col_headers(data, ...)

Arguments

data

a data frame

...

list of data to load into envrionment

Value

a data frame


A minimal data set of student subjects and grades

Description

A dataset containing the prices and other attributes of almost 54,000 diamonds.

Usage

student_df

Format

A data frame with 53940 rows and 10 variables:

subject
class
student
value
grade

...


A minimal data set of student subjects and grades

Description

A dataset containing the prices and other attributes of almost 54,000 diamonds.

Usage

student_df_1

Format

A data frame with 53940 rows and 10 variables:

subject
class
student
value
grade

...


A minimal data set of student subjects and grades

Description

A dataset containing the prices and other attributes of almost 54,000 diamonds.

Usage

student_df_2

Format

A data frame with 53940 rows and 10 variables:

subject
class
student
value
grade

...


Style the first col header

Description

Style the first col header

Usage

style_first_col_header(table, format)

Arguments

table

a data frame

format

a data frame

Value

a data frame


Style a spanner

Description

Style a spanner

Usage

style_spanner(table, format)

Arguments

table

an gt_tbl

format

formatting

Value

a data frame


Use attributes to construct a table with column and row headers.

Description

Use attributes to construct a table with column and row headers.

Usage

table_constructor(
  df,
  col_header_df,
  row_header_df,
  data_vars,
  table_name = NULL,
  table_format
)

Arguments

df

a data frame

col_header_df

col header information

row_header_df

row header information

data_vars

cell variable

table_name

name of the table

table_format

formatting o the table

Value

mmtable


Format a mmtable object.

Description

Format a mmtable object.

Usage

table_format(locations, ...)

Arguments

locations

indicating which cells should be formatted.

...

formating instructions

Value

format list

Examples

## Not run: 

library(tidyverse)

row_list <- cells_body(rows = c(1,3,5,7,9,11))
style_list <- list(cell_borders(sides = "top",color = "grey"))
gm_df <- gapminder_mm %>% dplyr::filter(var != "Life expectancy")

gm_table <-
  gm_df %>%
  mmtable(cells = value) +
  header_top(year) +
  header_left(country) +
  header_top_left(var)  +
  header_left_top(continent)  +
  table_format(
    locations = row_list,
    style = style_list)

## End(Not run)

Table source note

Description

Table source note

Usage

table_source_note(...)

Arguments

...

arguments of gt::tab_source_note

Value

format list


Add a table title

Description

Add a table title

Usage

table_title(...)

Arguments

...

arguments of gt::tab_header

Value

format list


A minimal data set of student subjects and grades

Description

A dataset containing the prices and other attributes of almost 54,000 diamonds.

Usage

table1

Format

A data frame with 53940 rows and 10 variables:

subject
class
student
value
grade

...


Transfer spanner from one table to another

Description

Transfer spanner from one table to another

Usage

transfer_spanner(gt_01, gt_02)

Arguments

gt_01

arguments of gt::tab_source_note

gt_02

arguments of gt::tab_source_note

Value

an mmtable