create_cross_table_header-methods: Method create_cross_table_header

create_cross_table_headerR Documentation

Method create_cross_table_header

Description

This function creates a styled header for a cross table. It is best used together with create_cross_table_body() and styled_table(). It assumes that the cross table consists of several Y columns that come first and has nested cross table part. That means that the cross table X columns can have more than one header level. For example: Male > Positive | Male > Negative | Female > Positive | Female > Negative In this case the 'Male' and 'Female' headings will be in merged cells above the 'Positive' and 'Negative' cells.

Usage

create_cross_table_header(y_col_headings, ...)

## S4 method for signature 'character'
create_cross_table_header(y_col_headings, cross_table_heading = NULL, ...)

Arguments

y_col_headings

A character vector that holds the header texts for the Y columns of the cross table. If no other arguments are given the resulting is not a cross table, but a normal table.

...

(optional) One or more vectors of character strings that hold the levels of the first/second/third/... cross table X column headings. These levels are used as header texts in the first/second/third/... row of the generated cross table columns. If no character vector is supplied, then the resulting table is not a cross table, but a normal table.

cross_table_heading

(optional) A single character string that is printed in a merged cell above the Y cross table cols.

Value

The generated StyledTable object holding the styled table header rows

Examples

## Not run: 
library(dplyr)
# prepare data set for cross table
students_data <- data.frame(
    country = rep(c("Germany", "Austria"), each = 16),
    year = c(rep("2010", 8), rep("2011", 8)),
    subject = rep(c(rep("Mathematics", 4), rep("Statistics", 4)), 2),
    gender = rep(c(rep("Male", 2), rep("Female", 2)), 4),
    result = rep(c("positive", "negative"), 16),
    N = sample(1:1000, 32)
  ) %>%
  group_by(country, year, subject, gender) %>%
  mutate(rel = 100 * N / sum(N)) %>%
  ungroup
# setup styled header
s_header <- create_cross_table_header(
    y_col_headings = c("Year", "Subject"),
    cross_table_heading = "Comparison of test results",
    c("Male", "Female"),
    c("Positive", "Negative"),
    c("count", "in %")
  ) %>%
  format_stat_header
# setup styled cross table body
s_body <- create_cross_table_body(
    data = students_data,
    sub_table_cols = "country",
    sub_heading_styling = function(st) set_horizontal(st, "center"),
    body_styling = function(st) set_horizontal(st, "center"),
    y_cols_left = c("year", "subject"),
    x_cols = c("gender", "result"),
    value_cols = c("N", "rel"),
    value_col_stylings = list(
      format_stat_absolute,
      format_stat_relative
    ),
    fill_values = list(0L, 0)
  )
# Concat styled header and styled body into a single styled table
s_tbl <- styled_table(
    s_header,
    s_body
  )
s_tbl %>%
  write_png

## End(Not run)

R-package/styledTables documentation built on Feb. 6, 2024, 2:21 a.m.