print_2x2_kable_table: Print a 2x2 table with kable

View source: R/print_2x2_kable_table.R

print_2x2_kable_tableR Documentation

Description

In my work with agreement statistics, I want to print a nice looking 2x2 table in the documents that I share with reaserchers. This function is a wrapper for the steps that I take to make these types of 2x2 tables.

Usage

print_2x2_kable_table(
  data,
  x,
  y,
  x_name = NULL,
  y_name = NULL,
  format = "html",
  full_width = FALSE,
  bootstrap_options = c("basic"),
  escape = TRUE,
  align = "lccc",
  caption = NULL,
  cols = NULL,
  widths = NULL,
  position = "center",
  font_size = NULL,
  row_label_position = "l",
  stripe_color = "gray!6",
  stripe_index = NULL,
  ...
)

Arguments

data

A data frame or tibble

x

"X" variable; counts appear in the rows of the table

y

"Y" variable; counts appear in the columns of the table

x_name

A string; name/label for the "X" variable

y_name

A string; name/label for the "Y" variable

format

A character string. Possible values are "latex", "html", "markdown", "pandoc", and "rst". This is an argument for knitr::kable()

full_width

A TRUE or FALSE variable controlling whether the HTML table should have 100\ the preferable format for full_width. If not specified, a HTML table will have full width by default but this option will be set to FALSE for a LaTeX table

bootstrap_options

A character vector for bootstrap table options. Please see package vignette or visit the w3schools' Bootstrap Page for more information. Possible options include basic, striped, bordered, hover, condensed, responsive and none.

escape

Boolean; whether to escape special characters when producing HTML or LaTeX tables. When escape = FALSE, you have to make sure that special characters will not trigger syntax errors in LaTeX or HTML.

align

Column alignment: a character vector consisting of 'l' (left), 'c' (center) and/or 'r' (right). By default or if align = NULL, numeric columns are right-aligned, and other columns are left-aligned. If length(align) == 1L, the string will be expanded to a vector of individual letters, e.g. 'clc' becomes c('c', 'l', 'c'), unless the output format is LaTeX.

caption

The table caption.

cols

A numeric value or vector indicating which column(s) to be selected.

widths

A character string telling HTML & LaTeX how wide the column needs to be, e.g. "10cm", "3in" or "30em".

position

A character string determining how to position the table on a page. Possible values include left, center, right, float_left and float_right. Please see the package doc site for demonstrations. For a LaTeX table, if float_* is selected, LaTeX package wrapfig will be imported.

font_size

A numeric input for table font size

row_label_position

A character string determining the justification of the row labels in a table. Possible values inclued l for left, c for center, and r for right. The default value is l for left justifcation.

stripe_color

LaTeX option allowing users to pick a different color for their strip lines. This option is not available in HTML

stripe_index

LaTeX option allowing users to customize which rows should have stripe color.

...

Other arguments passed to knitr::kable (see Examples and References).

Value

A character vector of the table source code.

Examples

library(dplyr)
library(ggplot2)
library(forcats)
library(janitor)

# Remove levels of some variables from the diamonds data frame so that it
# can be used as an example of a 2x2 table.
foo <- diamonds %>%
  dplyr::filter(color %in% c("E", "D")) %>%
  dplyr::filter(cut %in% c("Fair", "Good")) %>%
  dplyr::mutate(color = forcats::fct_drop(color),
                cut = forcats::fct_drop(cut))

# Tables using janitor::tabyl() and base::table()
foo %>% janitor::tabyl(color, cut)
table(foo$color, foo$cut)

# Example using the lamisc package
print_2x2_kable_table(data = foo,
                      x = color,
                      y = cut,
                      x_name = "COLOR",
                      y_name = "CUT")


emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.