pixiedust (version 0.9.4)

sprinkle: Define Customizations to a Table

Description

Customizations to a dust table are added by "sprinkling" with a little extra pixie dust. Sprinkles are a collection of attributes to be applied over a subset of table cells. They may be added to any part of the table, or to the table as a whole.

Usage

sprinkle(
  x,
  rows = NULL,
  cols = NULL,
  ...,
  part = c("body", "head", "foot", "interfoot", "table")
)

# S3 method for default sprinkle( x, rows = NULL, cols = NULL, ..., part = c("body", "head", "foot", "interfoot", "table"), fixed = FALSE, recycle = c("none", "rows", "cols", "columns") )

# S3 method for dust_list sprinkle( x, rows = NULL, cols = NULL, ..., part = c("body", "head", "foot", "interfoot", "table") )

sprinkle_print_method( x, print_method = c("console", "markdown", "html", "latex") )

# S3 method for default sprinkle_print_method( x, print_method = c("console", "markdown", "html", "latex", "docx") )

# S3 method for dust_list sprinkle_print_method( x, print_method = c("console", "markdown", "html", "latex") )

sprinkle_table(x, cols = NULL, ..., part = "table")

# S3 method for default sprinkle_table(x, cols = NULL, ..., part = "table")

# S3 method for dust_list sprinkle_table(x, cols = NULL, ..., part = "table")

Arguments

x

A dust object

rows

A numeric vector specifying the rows of the table to sprinkle. See details for more about sprinkling.

cols

A numeric (or character) vector specifying the columns (or column names) to sprinkle. See details for more about sprinkling.

...

named arguments, each of length 1, defining the customizations for the given cells. See "Sprinkles" for a listing of these arguments.

part

A character string denoting which part of the table to modify.

fixed

logical(1) indicating if the values in rows and cols should be read as fixed coordinate pairs. By default, sprinkles are applied at the intersection of rows and cols, meaning that the arguments do not have to share the same length. When fixed = TRUE, they must share the same length.

recycle

A character one that determines how sprinkles are managed when the sprinkle input doesn't match the length of the region to be sprinkled. By default, recycling is turned off. Recycling may be performed across rows first (left to right, top to bottom), or down columns first (top to bottom, left to right).

print_method

A character string giving the print method for the table. Note: "docx" is synonymous with "markdown".

Sprinkles

The following table describes the valid sprinkles that may be defined in the ... dots argument. All sprinkles may be defined for any output type, but only sprinkles recognized by that output type will be applied when printed. A more readable format of this information is available in vignette("sprinkles", package = "pixiedust").

bg
actionModifies the background color of a cell.
default
acceptsdvips color names; rgb(R,G,B); rgba(R,G,B,A);
#RRGGBB; #RRGGBBAA. See the "Colors" section
for further details.
consoleNot recognized
markdownNot recognized
htmlAccepts any of the listed formats;
recognizes transparency
latexAccepts any of the listed formats,
but ignores transparency
bg_pattern
actionGenerates a pattern of background colors.
Can be used to make striping
by rows or by columns.
defaultc("#FFFFFF", "#DDDDDD")
acceptsA vector of color names:
dvips color names; rgb(R,G,B); rgba(R,G,B,A);
#RRGGBB; #RRGGBBAA
consoleNot recognized
markdownNot recognized
htmlAccepts any of the listed formats;
recognizes transparency
latexAccepts any of the listed formats,
but ignores transparency
bg_pattern_by
actionDetermines if a `bg_pattern` is patterned
by row or by columns.
default"rows"
accepts"rows", "columns", "cols"
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
bold
actionRenders text within a cell in bold.
defaultFALSE
acceptslogical(1)
consoleRecognized; rendered as double asterisks on either
side of the text
markdownRecognized
htmlRecognized
latexRecognized
border_collapse
actionSets the `border-collapse` property in an
HTML table. The property sets whether the
table borders are collapsed into a
single border or detached as in standard HTML.
defaultTRUE
acceptslogical(1)
consoleNot recognized
markdownNot recognized
htmlRecognized
latexNot recognized
border
actionSets a border on the specified side of a cell.
default
acceptsAny combination of "all", "bottom", "left", "top",
"right". Using "all" results in all borders
being drawn, regardless of what other values are
passed with it.
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
border_color
actionSets the color of the borders specified for a cell.
default"Black"
acceptscharacter(1)
dvips color names; rgb(R,G,B); rgba(R,G,B,A);
#RRGGBB; #RRGGBBAA. See the "Colors" section
for further details.
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
border_style
actionSets the border style for a specified cell
default"solid"
acceptscharacter(1)
"solid", "dashed", "dotted", "double", "groove",
"ridge", "inset", "outset", "hidden", "none"
consoleNot recognized
markdownNot recognized
htmlAccepts any of the values listed.
latex; hhline = FALSEaccepts "solid", "dashed", "dotted",
"hidden", "none"
"dotted" is silently changed to "dashed"
"hidden" and "none" are equivalent.
latex; hhline = TRUEaccepts "solid", "double", "hidden", "none"
"hidden" and "none" are equivalent.
border_thickness
actionSets the thickness of the specified border
default1
acceptsnumeric(1)
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
border_units
actionSets the unit of measure for the specified border
thickness
default"pt"
accepts"pt", "px"
consoleNot recognized
markdownNot recognized
htmlRecognized
latexSilently changes "px" to "pt"
caption
actionAdds or alters the `caption` property
default
acceptscharacter(1)
consoleRecognized
markdownRecognized
htmlRecognized
latexRecognized
discrete
actionAdds distinct background colors based on
discrete values in the selected region.
May not be used concurrently with bg.
"font" is an alias for "font_color"
and "border" is an alias for
all borders.
default"bg"
accepts"bg", "font", "font_color", "border",
"left_border", "top_border", "right_border",
"bottom_border"
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
discrete_color
actionSets the color palette from which discrete
selects background colors. If NULL
colors are automatically selected using
the scales package.
defaultgetOption("pixie_discrete_pal", NULL)
acceptscharacter
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
float
actionSets the `float` property
defaultTRUE
acceptslogical(1)
consoleNot recognized
markdownNot recognized
htmlNot recognized
latexRecognized
fn
actionApplies a function to the value of a cell.
The function should be an
expression that acts on the variable `value`.
For example, quote(format(value, nsmall = 3))
default
acceptscall
consoleRecognized
markdownRecognized
htmlRecognized
latexRecognized
font_color
actionSets the color of the cell text
defaultBlack
acceptsdvips color names; rgb(R,G,B); rgba(R,G,B,A);
#RRGGBB; #RRGGBBAA. See the "Colors" section
for further details.
consoleNot recognized
markdownNot recognized
htmlRecognized; transparency recognized
latexRecognized; transparency ignored
font_family
actionSets the font for the text
defaultTimes New Roman
acceptscharacter(1)
http://www.w3schools.com/cssref/css_websafe_fonts.asp
consoleNot recognized
markdownNot recognized
htmlRecognized
latexNot recognized
font_size
actionSets the size of the font in the cell
default
acceptsnumeric(1)
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
font_size_units
actionDetermines the units in which `font_size`
is measured
default"px"
accepts"px", "pt", "%", "em"
consoleNot recognized
markdownNot recognized
htmlRecognized
latexOnly recognizes "pt" and "em".
All others are coerced to "pt"
gradient
actionAdds distinct background colors based on
progressively increasing values in the
selected region. May not be used concurrently
with bg.
"font" is an alias for "font_color"
and "border" is an alias for
all borders.
default"bg"
accepts"bg", "font", "font_color", "border",
"left_border", "top_border", "right_border",
"bottom_border"
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
gradient_colors
actionProvides the colors between which to
shade gradients.
defaultgetOptions("pixie_gradient_pal", NULL)
acceptscharacter
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
gradient_cut
actionDetermines the breaks points for the
gradient shading. When NULL
equally spaced quantiles are used, the
number of which are determined by
gradient_n.
defaultNULL
acceptsnumeric
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
gradient_n
actionDetermines the number of shades to use
between the colors in gradient_colors.
default10
acceptsnumeric
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
gradient_na
actionSets the color of NA values when gradients
are shaded.
defaultgrey
acceptscharacter(1)
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
halign
actionSets the horizontal alignment of the text in
the cell
default
accepts"left", "center", "right"
consoleNot recognized
markdownRecognized; numeric values will auto align to the
right if no value given.
htmlRecognized. Does not currently employ auto
alignment of numeric values, but this may change.
latexRecognized; numeric values will auto align to
the right if no value given.
height
actionSets the height of the cell
default
acceptsnumeric(1)
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
height_units
actionDetermines the units in which `height` is measured
default"pt"
accepts"px", "pt", "cm", "in", "%"
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized; "px" is coerced to "pt"
hhline
actionToggles the option for cell border drawing with
the `hhline` LaTeX package
defaultFALSE
acceptslogical(1)
consoleNot recognized
markdownNot recognized
htmlNot recognized
latexRecognized. When `FALSE` double borders are
not available.
When `TRUE`, colored and dashed borders are not
available. This is usually the better option
when using colored backgrounds in table cells.
italic
actionRenders the text in the cell in italic
defaultFALSE
acceptslogical(1)
consoleRecognized; rendered as an underscore on either
side of the text.
markdownRecognized
htmlRecognized
latexRecognized
justify
actionJustifies the entire table on the page.
default"center"
acceptscharacter(1)
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognizes "center", but both "left" and "right"
are rendered as left justified. This may change
if a satisfactory solution is found.
Usually, tables are best left centered.
longtable
actionToggles the use of the LaTeX `longtable` style
tables, namely allowing long tables to be broken
into multiple sections. The table header appears
at the top of each section. The table interfoot
appears at the bottom of each section, except
for the last.
The table foot appears at the bottom of the
last section.
May accept either a logical or a numerical value.
If numerical, each section will have the specified
number of rows.
defaultFALSE
acceptslogical(1); numeric(1)
consoleRecognized; when `TRUE`, defaults to 25 rows
per section.
markdownRecognized; when `TRUE`, defaults to 25 rows
per section.
htmlRecognized; when `TRUE`, defaults to 25 rows
per section.
latexRecognized; when `TRUE`, `longtable`'s own algorithm
will determine the number of rows per section.
When numeric, breaks are forced at the specified
number of rows.
merge
actionMerges cells in the specified range into a
single cell. In cases where
either `merge_rowval` or `merge_colval` is
specified, they will only be
honored if `merge = TRUE`. You must opt in to
this action.
defaultFALSE
acceptslogical(1)
consoleRecognized
markdownRecognized
htmlRecognized
latexRecognized
merge_rowval
actionSpecifies the row value of the merged range to
print in the table
defaultminimum row value of the merged range
acceptsnumeric(1)
consoleRecognized
markdownRecognized
htmlRecognized
latexRecognized
merge_colval
actionSpecifies the column value of the merged range
to print in the table
defaultminimum col value of the merged range
acceptsnumeric(1)
consoleRecognized
markdownRecognized
htmlRecognized
latexRecognized
na_string
actionDesignates the character string to use in place
of missing values
defaultNA
acceptscharacter(1)
consoleRecognized
markdownRecognized
htmlRecognized
latexRecognized
pad
actionDesignates the padding to place between cell
text and boundaries
Measured in pixels.
default0
acceptsnumeric(1)
consoleNot recognized
markdownNot recognized
htmlRecognized
latexNot recognized
replace
actionReplaces existing cell values with user-specified
content. Replacement occurs moving down columns
from left to right.
default
acceptscharacter vector of the same length as the number
of cells being replaced.
consoleRecognized
markdownRecognized
htmlRecognized
latexRecognized
rotate_degree
actionRotates text in cells by the designated angle
in degrees
default
acceptsnumeric(1)
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
round
actionApplies the `round` function to values in the
cell. Skips any character values it encounters.
defaultgetOption("digits")
acceptsnumeric(1)
consoleRecognized
markdownRecognized
htmlRecognized
latexRecognized
sanitize
actionSanitizes character values that may cause
difficulties for the rendered format.
defaultFALSE
acceptslogical(1)
consoleNot recognized
markdownNot recognized
htmlNot recognized
latexRecognized. Sanitization is performed using
latexTranslate
sanitize_args
actionPasses additional arguments to latexTranslate
defaultlist()
acceptslist. See documentation for latexTranslate
for details
consoleNot recognized
markdownNot recognized
htmlNot recognized
latexRecognized
tabcolsep
actionModifies the LaTeX `tabcolsep` parameter of tables
This is similar to `pad` for HTML tables, but only
affects the space between columns. Measured in "pt"
default6
acceptsnumeric(1)
consoleNot recognized
markdownNot recognized
htmlNot recognized
latexRecognized
valign
actionDesignates the vertical alignment of a cell.
default
accepts"top", "middle", "bottom"
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
width
actionSets the width of the cell
default
acceptsnumeric(1)
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized
width_units
actionDetermines the units in which `width` is measured
default"pt"
accepts"px", "pt", "cm", "in", "%"
consoleNot recognized
markdownNot recognized
htmlRecognized
latexRecognized; "px" is coerced to "pt"

Longtable

The longtable feature is named for the LaTeX package used to break very large tables into multiple pages.

When using the longtable=TRUE option, the default number of rows per table is 25 for console, HTML, and markdown output. For LaTeX output, the number of rows is determined by the LaTeX longtable package's algorithm. The number of rows per table only considers the content in the body of the table. Consideration for the number of rows in the head and foot are the responsibility of the user.

Whenever a table is broken into multiple parts, each part retains the table head. If any interfoot is provided, it is appended to the bottom of each section, with the exception of the last section. The last section has the foot appended.

Colors

Colors may be declared as any of the color names in colors(), as rgb character strings such as "rgb(rrr,ggg,bbb)" or as hexadecimal character strings such as "#rrggbb".

Transparency is also recognized by HTML output, and may be indicated in the rgba format "rgba(rrr,ggg,bbb,aa)", where aa is a number between 0 and 1, inclusive. Alternative, transparency may be given as "#rrggbbAA", where AA is a hexadecimal representation of transparency with "00" being completely transparent and "FF" being completely opaque.

LaTeX output does not recognize transparency and will quietly drop the transparency parameter.

All colors are internally translated into rgb format and are case insensitive.

Required LaTeX Packages

If you will be using the LaTeX output, some sprinkles will require you to include additional LaTeX packages in your document preamble. In .Rnw files, additional packages can be included with the \usepackage{[package]} syntax. In markdown, additional packages are included using header-includes: in the YAML front matter with a line of the format \usepackage{[package]} for each package to be used. Sprinkles that require additional packages, and the LaTeX packages required, are listed below:

SprinkleLaTeX Package(s)
font_color\usepackage[dvipsnames]{xcolor}
bg, bg_pattern\usepackage[dvipsnames,table]{xcolor}
border_style\usepackage{arydshln}
\usepackage{amssymb}
\usepackage{hhline}
(with vertical dashed lines)\usepackage{graphicx}
\makeatletter
\newcommand*\vdashline{\rotatebox[origin=c]{90}{\$\dabar@\dabar@\dabar@\$}}
\makeatother
longtable\usepackage{longtable}
(Must be loaded before arydshln)
merge\usepackage{multirow}
captions for non floats\usepackage{caption}

Note that hhline is used to make horizontal lines when options(pixiedust_latex_hhline = TRUE) (the package default is FALSE), otherwise the cline command is used.

Use of cline permits colored borders and dashed borders, but borders around cells with background colors are sometimes (often) lost.

Use of hhline preserves borders around cells with background colors and permits double borders, but colored and dashed borders are not available.

In order to ensure all features are available, the recommended code block (accounting for the proper order to load packages) is:

header-includes:
- \usepackage{amssymb}
- \usepackage{arydshln}
- \usepackage{caption}
- \usepackage{graphicx}
- \usepackage{hhline}
- \usepackage{longtable}
- \usepackage{multirow}
- \usepackage[dvipsnames,table]{xcolor}
- \makeatletter
- \newcommand*\vdashline{\rotatebox[origin=c]{90}{\$\dabar@\dabar@\dabar@\$}}
- \makeatother

Author

Benjamin Nutter

Details

Sprinkling is done over the intersection of rows and columns (unless fixed = TRUE. If rows but no columns are specified, sprinkling is performed over all columns of the given given rows. The reverse is true for when columns but no rows are specified. If neither columns nor rows are specified, the attribute is applied over all of the cells in the table part denoted in part.

If at least one of border, border_thickness, border_units, border_style or border_color is specified, the remaining unspecified attributes assume their default values.

Other sprinkle pairings are height and height_units; width and width_units; font_size and font_size_units; bg_pattern and bg_pattern_by

The sprinkles bg and bg_pattern may not be used together.

A more detailed demonstration of the use of sprinkles is available in vignette("pixiedust", package = "pixiedust")

Using sprinkle_table, sprinkles may be applied to the columns of multiple tables. Table parts are required to have the same number of columns, but not necessarily the same number of rows, which is why the rows argument is not available for the sprinkle_table. In contrast to sprinkle, the part argument in sprinkle_table will accept multiple parts. If any of the named parts is "table", the sprinkle will be applied to the columns of all of the parts.

See Also

sprinkle_colnames for changing column names in a table.

Examples

Run this code
x <- dust(lm(mpg ~ qsec + factor(am), data = mtcars))
x %>% sprinkle(cols = 2:4, round = 3) %>% 
  sprinkle(cols = 5, fn = quote(pvalString(value))) %>% 
  sprinkle(rows = 2, bold = TRUE)

Run the code above in your browser using DataLab