blob: 80c318a0c00275449c7d2c679406516eb92360ad [file] [log] [blame]
\documentclass[table]{article}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\else % if luatex or xelatex
\ifxetex
\usepackage{mathspec}
\else
\usepackage{fontspec}
\fi
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\hypersetup{unicode=true,
pdftitle={Create Awesome LaTeX Table with knitr::kable and kableExtra},
pdfauthor={Hao Zhu},
pdfborder={0 0 0},
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}
%%% Change title format to be more compact
\usepackage{titling}
% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
\posttitle{
\begin{center}\large#1\end{center}
}
}
\setlength{\droptitle}{-2em}
\title{Create Awesome LaTeX Table with knitr::kable and kableExtra}
\pretitle{\vspace{\droptitle}\centering\huge}
\posttitle{\par}
\author{Hao Zhu}
\preauthor{\centering\large\emph}
\postauthor{\par}
\predate{\centering\large\emph}
\postdate{\par}
\date{2019-01-06}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\begin{document}
\maketitle
{
\setcounter{tocdepth}{2}
\tableofcontents
}
\clearpage
\begin{quote}
Please see the package
\href{http://haozhu233.github.io/kableExtra}{documentation site} for how
to use this package in HTML and more.
\end{quote}
\hypertarget{overview}{%
\section{Overview}\label{overview}}
\begin{wrapfigure}{r}{0.2\textwidth}\centering
\includegraphics{kableExtra_sm.png}
\end{wrapfigure}
The goal of \texttt{kableExtra} is to help you build common complex
tables and manipulate table styles. It imports the pipe
\texttt{\%\textgreater{}\%} symbol from \texttt{magrittr} and verbalizes
all the functions, so basically you can add ``layers'' to a kable output
in a way that is similar with \texttt{ggplot2} and \texttt{plotly}.
To learn how to generate complex tables in HTML, please visit
\url{http://haozhu233.github.io/kableExtra/awesome_table_in_html.html}.
\hypertarget{installation}{%
\section{Installation}\label{installation}}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"kableExtra"}\NormalTok{)}
\CommentTok{# For dev version}
\CommentTok{# install.packages("devtools")}
\NormalTok{devtools}\OperatorTok{::}\KeywordTok{install_github}\NormalTok{(}\StringTok{"haozhu233/kableExtra"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\hypertarget{getting-started}{%
\section{Getting Started}\label{getting-started}}
Here we are using the first few columns and rows from dataset
\texttt{mtcars}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(knitr)}
\KeywordTok{library}\NormalTok{(kableExtra)}
\NormalTok{dt <-}\StringTok{ }\NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{6}\NormalTok{]}
\end{Highlighting}
\end{Shaded}
When you are using \texttt{kable()}, if you don't specify
\texttt{format}, by default it will generate a markdown table and let
pandoc handle the conversion from markdown to HTML/PDF. This is the most
favorable approach to render most simple tables as it is format
independent. If you switch from HTML to pdf, you basically don't need to
change anything in your code. However, markdown doesn't support complex
table. For example, if you want to have a double-row header table,
markdown just cannot provide you the functionality you need. As a
result, when you have such a need, you should \textbf{define
\texttt{format} in \texttt{kable()}} as either ``html'' or ``latex''.
\emph{You can also define a global option at the beginning using
\texttt{options(knitr.table.format\ =\ "latex")} so you don't repeat the
step every time.} \textbf{In this tutorial, I'll still put
format=``latex'' in the function in case users just want to quickly
replicate the results. In practice, you don't need to define those
formats.}
\textbf{Starting from \texttt{kableExtra} 0.9.0}, when you load this
package (\texttt{library(kableExtra)}),
\textcolor{red}{\textbf{it will automatically set up the global option 'knitr.table.format' based on your current environment}}.
Unless you are rendering a PDF, \texttt{kableExtra} will try to render a
HTML table for you. \textbf{You no longer need to manually set either
the global option or the \texttt{format} option in each \texttt{kable()}
function}. I'm still including the explanation above here in this
vignette so you can understand what is going on behind the scene. Note
that this is only an global option. You can manually set any format in
\texttt{kable()} whenever you want. I just hope you can enjoy a peace of
mind in most of your time.
You can disable this behavior by setting
\texttt{options(kableExtra.auto\_format\ =\ FALSE)} before you load
\texttt{kableExtra}.
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# If you are using kableExtra < 0.9.0, you are recommended to set a global option first.}
\CommentTok{# options(knitr.table.format = "latex") }
\CommentTok{## If you don't define format here, you'll need put `format = "latex"` }
\CommentTok{## in every kable function.}
\end{Highlighting}
\end{Shaded}
\hypertarget{latex-packages-used-in-this-package}{%
\subsection{LaTeX packages used in this
package}\label{latex-packages-used-in-this-package}}
If you are using a recent version of rmarkdown, you are recommended to
load this package entirely via \texttt{library(kableExtra)} or
\texttt{require(kableExtra)} because this package will load all
necessary LaTeX packages, such as \texttt{booktabs} or
\texttt{multirow}, for you automatically. Note that, if you are calling
functions from \texttt{kableExtra} via
\texttt{kableExtra::kable\_styling()} or if you put
\texttt{library(kableExtra)} in a separate R file that is
\textbf{sourced} by the rmarkdown document, these packages won't be
loaded. Furthermore, you can suppress this auto-loading behavior by
setting a global option \texttt{kableExtra.latex.load\_packages} to be
\texttt{FALSE} before you load \texttt{kableExtra}.
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Not evaluated. Ilustration purpose}
\KeywordTok{options}\NormalTok{(}\DataTypeTok{kableExtra.latex.load_packages =} \OtherTok{FALSE}\NormalTok{)}
\KeywordTok{library}\NormalTok{(kableExtra)}
\end{Highlighting}
\end{Shaded}
If you are using R Sweave, beamer, R package vignette template, tufte or
some customized rmarkdown templates, you can put the following meta data
into the \texttt{yaml} section. If you are familar with LaTeX and you
know what you are doing, feel free to remove unnecessary packages from
the list.
\begin{verbatim}
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{array}
- \usepackage{multirow}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{pdflscape}
- \usepackage{tabu}
- \usepackage{threeparttable}
- \usepackage{threeparttablex}
- \usepackage[normalem]{ulem}
- \usepackage{makecell}
\end{verbatim}
Note: \texttt{kableExtra} was using \texttt{xcolor} for alternative row
color before 1.0. However, the recent updates in \texttt{fancyvbr}
causes a clash in \texttt{xcolor} option. Therefore, we removed the
\texttt{xcolor} dependency in version 1.0 and started to rely on
\texttt{colortbl} completely. If you experience any issues, please
report on github.
\hypertarget{plain-latex}{%
\subsection{Plain LaTeX}\label{plain-latex}}
Plain LaTeX table looks relatively ugly in 2017.
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Again, with kableExtra >= 0.9.0, `format = "latex"` is automatically defined}
\CommentTok{# when this package gets loaded. Otherwise, you still need to define formats}
\KeywordTok{kable}\NormalTok{(dt)}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{l|r|r|r|r|r|r}
\hline
& mpg & cyl & disp & hp & drat & wt\\
\hline
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
\hline
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
\hline
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
\hline
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
\hline
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\hline
\end{tabular}
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Same: kable(dt, "latex")}
\end{Highlighting}
\end{Shaded}
\hypertarget{latex-table-with-booktabs}{%
\subsection{LaTeX table with booktabs}\label{latex-table-with-booktabs}}
Similar to Bootstrap in HTML, in LaTeX, you can also use a trick to make
your table look prettier as well. The different part is that, this time
you don't need to pipe kable outputs to another function. Instead, you
should call \texttt{booktabs\ =\ T} directly in \texttt{kable()}.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{lrrrrrr}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
\hypertarget{table-styles}{%
\section{Table Styles}\label{table-styles}}
\texttt{kable\_styling} in LaTeX uses the same syntax and structure as
\texttt{kable\_styling} in HTML. However, instead of
\texttt{bootstrap\_options}, you should specify \texttt{latex\_options}
instead.
\hypertarget{latex-options}{%
\subsection{LaTeX options}\label{latex-options}}
Similar with \texttt{bootstap\_options}, \texttt{latex\_options} is also
a charter vector with a bunch of options including \texttt{striped},
\texttt{hold\_position} and \texttt{scale\_down}.
\hypertarget{striped}{%
\subsubsection{Striped}\label{striped}}
Even though in the LaTeX world, people usually call it
\texttt{alternative\ row\ colors} but here I'm using its bootstrap name
for consistency. Note that to make it happen, LaTeX package
\texttt{xcolor} is required to be loaded. In an environment like
rmarkdown::pdf\_document (rmarkdown 1.4.0 +), \texttt{kable\_styling}
will load it automatically if \texttt{striped} is enabled. However, in
other cases, you probably need to import that package by yourself.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \StringTok{"striped"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering\rowcolors{2}{gray!6}{white}
\begin{tabular}{lrrrrrr}
\hiderowcolors
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
\showrowcolors
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
\rowcolors{2}{white}{white}
\end{table}
\hypertarget{hold-position}{%
\subsubsection{Hold position}\label{hold-position}}
If you provide a table caption in \texttt{kable()}, it will put your
LaTeX tabular in a \texttt{table} environment, unless you are using
\texttt{longtable}. A \texttt{table} environment will automatically find
the best place (it thinks) to put your table. However, in many cases,
you do want your table to appear in a position you want it to be. In
this case, you can use this \texttt{hold\_position} options here.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{caption =} \StringTok{"Demo table"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{, }\StringTok{"hold_position"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\rowcolors{2}{gray!6}{white}
\begin{table}[!h]
\caption{\label{tab:unnamed-chunk-8}Demo table}
\centering
\begin{tabular}{lrrrrrr}
\hiderowcolors
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
\showrowcolors
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
\end{table}
\rowcolors{2}{white}{white}
If you find \texttt{hold\_position} is not powerful enough to literally
PIN your table in the exact position, you may want to use
\texttt{HOLD\_position}, which is a more powerful version of this
feature. For those who are familiar with LaTeX, \texttt{hold\_position}
uses \texttt{{[}!h{]}} and \texttt{HOLD\_position} uses \texttt{{[}H{]}}
and the \texttt{float} package.
\hypertarget{scale-down}{%
\subsubsection{Scale down}\label{scale-down}}
When you have a wide table that will normally go out of the page, and
you want to scale down the table to fit the page, you can use the
\texttt{scale\_down} option here. Note that, if your table is too small,
it will also scale up your table. It was named in this way only because
scaling up isn't very useful in most cases.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(}\KeywordTok{cbind}\NormalTok{(dt, dt, dt), }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{, }\StringTok{"scale_down"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering\rowcolors{2}{gray!6}{white}
\resizebox{\linewidth}{!}{
\begin{tabular}{lrrrrrrrrrrrrrrrrrr}
\hiderowcolors
\toprule
& mpg & cyl & disp & hp & drat & wt & mpg & cyl & disp & hp & drat & wt & mpg & cyl & disp & hp & drat & wt\\
\midrule
\showrowcolors
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}}
\rowcolors{2}{white}{white}
\end{table}
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(}\KeywordTok{cbind}\NormalTok{(dt), }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{, }\StringTok{"scale_down"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering\rowcolors{2}{gray!6}{white}
\resizebox{\linewidth}{!}{
\begin{tabular}{lrrrrrr}
\hiderowcolors
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
\showrowcolors
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}}
\rowcolors{2}{white}{white}
\end{table}
\hypertarget{repeat-header-in-longtable}{%
\subsubsection{Repeat header in
longtable}\label{repeat-header-in-longtable}}
In \texttt{kableExtra} 0.3.0 or above, a new option
\texttt{repeat\_header} was introduced into \texttt{kable\_styling}. It
will add header rows to longtables spanning multiple pages. For table
captions on following pages, it will append \emph{``continued''} to the
caption to differentiate. If you need texts other than
\emph{``(continued)''} (for example, other languages), you can specify
it using \texttt{kable\_styling(...,\ repeat\_header\_text\ =\ "xxx")}.
If you want to completely replace the table caption instead of
appending, you can specify it in the option
\texttt{repeat\_header\_method}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{long_dt <-}\StringTok{ }\KeywordTok{rbind}\NormalTok{(mtcars, mtcars) }
\KeywordTok{kable}\NormalTok{(long_dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{longtable =}\NormalTok{ T, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{caption =} \StringTok{"Longtable"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group 1"}\NormalTok{ =}\StringTok{ }\DecValTok{5}\NormalTok{, }\StringTok{"Group 2"}\NormalTok{ =}\StringTok{ }\DecValTok{6}\NormalTok{)) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"repeat_header"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{longtable}{lrrrrrrrrrrr}
\caption{\label{tab:unnamed-chunk-11}Longtable}\\
\toprule
\multicolumn{1}{c}{ } & \multicolumn{5}{c}{Group 1} & \multicolumn{6}{c}{Group 2} \\
\cmidrule(l{3pt}r{3pt}){2-6} \cmidrule(l{3pt}r{3pt}){7-12}
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
\endfirsthead
\caption[]{Longtable \textit{(continued)}}\\
\toprule
\multicolumn{1}{c}{ } & \multicolumn{5}{c}{Group 1} & \multicolumn{6}{c}{Group 2} \\
\cmidrule(l{3pt}r{3pt}){2-6} \cmidrule(l{3pt}r{3pt}){7-12}
& mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
\midrule
\endhead
\
\endfoot
\bottomrule
\endlastfoot
Mazda RX4 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4\\
Mazda RX4 Wag & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4\\
Datsun 710 & 22.8 & 4 & 108.0 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1\\
Hornet 4 Drive & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1\\
Hornet Sportabout & 18.7 & 8 & 360.0 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2\\
\addlinespace
Valiant & 18.1 & 6 & 225.0 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1\\
Duster 360 & 14.3 & 8 & 360.0 & 245 & 3.21 & 3.570 & 15.84 & 0 & 0 & 3 & 4\\
Merc 240D & 24.4 & 4 & 146.7 & 62 & 3.69 & 3.190 & 20.00 & 1 & 0 & 4 & 2\\
Merc 230 & 22.8 & 4 & 140.8 & 95 & 3.92 & 3.150 & 22.90 & 1 & 0 & 4 & 2\\
Merc 280 & 19.2 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.30 & 1 & 0 & 4 & 4\\
\addlinespace
Merc 280C & 17.8 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.90 & 1 & 0 & 4 & 4\\
Merc 450SE & 16.4 & 8 & 275.8 & 180 & 3.07 & 4.070 & 17.40 & 0 & 0 & 3 & 3\\
Merc 450SL & 17.3 & 8 & 275.8 & 180 & 3.07 & 3.730 & 17.60 & 0 & 0 & 3 & 3\\
Merc 450SLC & 15.2 & 8 & 275.8 & 180 & 3.07 & 3.780 & 18.00 & 0 & 0 & 3 & 3\\
Cadillac Fleetwood & 10.4 & 8 & 472.0 & 205 & 2.93 & 5.250 & 17.98 & 0 & 0 & 3 & 4\\
\addlinespace
Lincoln Continental & 10.4 & 8 & 460.0 & 215 & 3.00 & 5.424 & 17.82 & 0 & 0 & 3 & 4\\
Chrysler Imperial & 14.7 & 8 & 440.0 & 230 & 3.23 & 5.345 & 17.42 & 0 & 0 & 3 & 4\\
Fiat 128 & 32.4 & 4 & 78.7 & 66 & 4.08 & 2.200 & 19.47 & 1 & 1 & 4 & 1\\
Honda Civic & 30.4 & 4 & 75.7 & 52 & 4.93 & 1.615 & 18.52 & 1 & 1 & 4 & 2\\
Toyota Corolla & 33.9 & 4 & 71.1 & 65 & 4.22 & 1.835 & 19.90 & 1 & 1 & 4 & 1\\
\addlinespace
Toyota Corona & 21.5 & 4 & 120.1 & 97 & 3.70 & 2.465 & 20.01 & 1 & 0 & 3 & 1\\
Dodge Challenger & 15.5 & 8 & 318.0 & 150 & 2.76 & 3.520 & 16.87 & 0 & 0 & 3 & 2\\
AMC Javelin & 15.2 & 8 & 304.0 & 150 & 3.15 & 3.435 & 17.30 & 0 & 0 & 3 & 2\\
Camaro Z28 & 13.3 & 8 & 350.0 & 245 & 3.73 & 3.840 & 15.41 & 0 & 0 & 3 & 4\\
Pontiac Firebird & 19.2 & 8 & 400.0 & 175 & 3.08 & 3.845 & 17.05 & 0 & 0 & 3 & 2\\
\addlinespace
Fiat X1-9 & 27.3 & 4 & 79.0 & 66 & 4.08 & 1.935 & 18.90 & 1 & 1 & 4 & 1\\
Porsche 914-2 & 26.0 & 4 & 120.3 & 91 & 4.43 & 2.140 & 16.70 & 0 & 1 & 5 & 2\\
Lotus Europa & 30.4 & 4 & 95.1 & 113 & 3.77 & 1.513 & 16.90 & 1 & 1 & 5 & 2\\
Ford Pantera L & 15.8 & 8 & 351.0 & 264 & 4.22 & 3.170 & 14.50 & 0 & 1 & 5 & 4\\
Ferrari Dino & 19.7 & 6 & 145.0 & 175 & 3.62 & 2.770 & 15.50 & 0 & 1 & 5 & 6\\
\addlinespace
Maserati Bora & 15.0 & 8 & 301.0 & 335 & 3.54 & 3.570 & 14.60 & 0 & 1 & 5 & 8\\
Volvo 142E & 21.4 & 4 & 121.0 & 109 & 4.11 & 2.780 & 18.60 & 1 & 1 & 4 & 2\\
Mazda RX41 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4\\
Mazda RX4 Wag1 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4\\
Datsun 7101 & 22.8 & 4 & 108.0 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1\\
\addlinespace
Hornet 4 Drive1 & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1\\
Hornet Sportabout1 & 18.7 & 8 & 360.0 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2\\
Valiant1 & 18.1 & 6 & 225.0 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1\\
Duster 3601 & 14.3 & 8 & 360.0 & 245 & 3.21 & 3.570 & 15.84 & 0 & 0 & 3 & 4\\
Merc 240D1 & 24.4 & 4 & 146.7 & 62 & 3.69 & 3.190 & 20.00 & 1 & 0 & 4 & 2\\
\addlinespace
Merc 2301 & 22.8 & 4 & 140.8 & 95 & 3.92 & 3.150 & 22.90 & 1 & 0 & 4 & 2\\
Merc 2801 & 19.2 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.30 & 1 & 0 & 4 & 4\\
Merc 280C1 & 17.8 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.90 & 1 & 0 & 4 & 4\\
Merc 450SE1 & 16.4 & 8 & 275.8 & 180 & 3.07 & 4.070 & 17.40 & 0 & 0 & 3 & 3\\
Merc 450SL1 & 17.3 & 8 & 275.8 & 180 & 3.07 & 3.730 & 17.60 & 0 & 0 & 3 & 3\\
\addlinespace
Merc 450SLC1 & 15.2 & 8 & 275.8 & 180 & 3.07 & 3.780 & 18.00 & 0 & 0 & 3 & 3\\
Cadillac Fleetwood1 & 10.4 & 8 & 472.0 & 205 & 2.93 & 5.250 & 17.98 & 0 & 0 & 3 & 4\\
Lincoln Continental1 & 10.4 & 8 & 460.0 & 215 & 3.00 & 5.424 & 17.82 & 0 & 0 & 3 & 4\\
Chrysler Imperial1 & 14.7 & 8 & 440.0 & 230 & 3.23 & 5.345 & 17.42 & 0 & 0 & 3 & 4\\
Fiat 1281 & 32.4 & 4 & 78.7 & 66 & 4.08 & 2.200 & 19.47 & 1 & 1 & 4 & 1\\
\addlinespace
Honda Civic1 & 30.4 & 4 & 75.7 & 52 & 4.93 & 1.615 & 18.52 & 1 & 1 & 4 & 2\\
Toyota Corolla1 & 33.9 & 4 & 71.1 & 65 & 4.22 & 1.835 & 19.90 & 1 & 1 & 4 & 1\\
Toyota Corona1 & 21.5 & 4 & 120.1 & 97 & 3.70 & 2.465 & 20.01 & 1 & 0 & 3 & 1\\
Dodge Challenger1 & 15.5 & 8 & 318.0 & 150 & 2.76 & 3.520 & 16.87 & 0 & 0 & 3 & 2\\
AMC Javelin1 & 15.2 & 8 & 304.0 & 150 & 3.15 & 3.435 & 17.30 & 0 & 0 & 3 & 2\\
\addlinespace
Camaro Z281 & 13.3 & 8 & 350.0 & 245 & 3.73 & 3.840 & 15.41 & 0 & 0 & 3 & 4\\
Pontiac Firebird1 & 19.2 & 8 & 400.0 & 175 & 3.08 & 3.845 & 17.05 & 0 & 0 & 3 & 2\\
Fiat X1-91 & 27.3 & 4 & 79.0 & 66 & 4.08 & 1.935 & 18.90 & 1 & 1 & 4 & 1\\
Porsche 914-21 & 26.0 & 4 & 120.3 & 91 & 4.43 & 2.140 & 16.70 & 0 & 1 & 5 & 2\\
Lotus Europa1 & 30.4 & 4 & 95.1 & 113 & 3.77 & 1.513 & 16.90 & 1 & 1 & 5 & 2\\
\addlinespace
Ford Pantera L1 & 15.8 & 8 & 351.0 & 264 & 4.22 & 3.170 & 14.50 & 0 & 1 & 5 & 4\\
Ferrari Dino1 & 19.7 & 6 & 145.0 & 175 & 3.62 & 2.770 & 15.50 & 0 & 1 & 5 & 6\\
Maserati Bora1 & 15.0 & 8 & 301.0 & 335 & 3.54 & 3.570 & 14.60 & 0 & 1 & 5 & 8\\
Volvo 142E1 & 21.4 & 4 & 121.0 & 109 & 4.11 & 2.780 & 18.60 & 1 & 1 & 4 & 2\\*
\end{longtable}
\hypertarget{full-width}{%
\subsection{Full width?}\label{full-width}}
If you have a small table and you want it to spread wide on the page,
you can try the \texttt{full\_width} option. Unlike
\texttt{scale\_down}, it won't change your font size. You can use
\texttt{column\_spec}, which will be explained later, together with
\texttt{full\_width} to achieve the best result.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{full_width =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{width =} \StringTok{"8cm"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{tabu} to \linewidth {>{\raggedright\arraybackslash}p{8cm}>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabu}
\hypertarget{position}{%
\subsection{Position}\label{position}}
Table Position only matters when the table doesn't have
\texttt{full\_width}. You can choose to align the table to
\texttt{center} or \texttt{left} side of the page. The default value of
position is \texttt{center}.
Note that even though you can select to \texttt{right} align your table
but the table will actually be centered. Somehow it is very difficult to
right align a table in LaTeX (since it's not very useful in the real
world?). If you know how to do it, please send out an issue or PR and
let me know.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{position =} \StringTok{"center"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering
\begin{tabular}{lrrrrrr}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
\end{table}
Becides these three common options, you can also wrap text around the
table using the \texttt{float-left} or \texttt{float-right} options.
Note that, like \texttt{striped}, this feature will load another
non-default LaTeX package \texttt{wrapfig} which requires rmarkdown
1.4.0 +. If you rmarkdown version \textless{} 1.4.0, you need to load
the package through a customed LaTeX template file.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{position =} \StringTok{"float_right"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{wraptable}{r}{0pt}
\begin{tabular}{lrrrrrr}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}\end{wraptable}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sit amet
mauris in ex ultricies elementum vel rutrum dolor. Phasellus tempor
convallis dui, in hendrerit mauris placerat scelerisque. Maecenas a
accumsan enim, a maximus velit. Pellentesque in risus eget est faucibus
convallis nec at nulla. Phasellus nec lacinia justo. Morbi fermentum,
orci id varius accumsan, nibh neque porttitor ipsum, consectetur luctus
risus arcu ac ex. Aenean a luctus augue. Suspendisse et auctor nisl.
Suspendisse cursus ultrices quam non vulputate. Phasellus et pharetra
neque, vel feugiat erat. Sed feugiat elit at mauris commodo consequat.
Sed congue lectus id mattis hendrerit. Mauris turpis nisl, congue eget
velit sed, imperdiet convallis magna. Nam accumsan urna risus, non
feugiat odio vehicula eget.
\hypertarget{font-size}{%
\subsection{Font Size}\label{font-size}}
If one of your tables is huge and you want to use a smaller font size
for that specific table, you can use the \texttt{font\_size} option.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{font_size =} \DecValTok{7}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering\begingroup\fontsize{7}{9}\selectfont
\begin{tabular}{lrrrrrr}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
\endgroup{}
\end{table}
\hypertarget{column-row-specification}{%
\section{Column / Row Specification}\label{column-row-specification}}
\hypertarget{column-spec}{%
\subsection{Column spec}\label{column-spec}}
When you have a table with lots of explanatory texts, you may want to
specify the column width for different column, since the auto adjust in
HTML may not work in its best way while basic LaTeX table is really bad
at handling text wrapping. Also, sometimes, you may want to highlight a
column (e.g., a ``Total'' column) by making it bold. In these scenarios,
you can use \texttt{column\_spec()}. You can find an example below.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{text_tbl <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}
\DataTypeTok{Items =} \KeywordTok{c}\NormalTok{(}\StringTok{"Item 1"}\NormalTok{, }\StringTok{"Item 2"}\NormalTok{, }\StringTok{"Item 3"}\NormalTok{),}
\DataTypeTok{Features =} \KeywordTok{c}\NormalTok{(}
\StringTok{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. "}\NormalTok{,}
\StringTok{"In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. "}\NormalTok{, }
\StringTok{"Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. "}
\NormalTok{ )}
\NormalTok{)}
\KeywordTok{kable}\NormalTok{(text_tbl, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{full_width =}\NormalTok{ F) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{color =} \StringTok{"red"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DataTypeTok{width =} \StringTok{"30em"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering
\begin{tabular}{>{\bfseries\leavevmode\color{red}}l>{\raggedright\arraybackslash}p{30em}}
\toprule
Items & Features\\
\midrule
Item 1 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a.\\
Item 2 & In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in.\\
Item 3 & Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis.\\
\bottomrule
\end{tabular}
\end{table}
\hypertarget{row-spec}{%
\subsection{Row spec}\label{row-spec}}
Similar with \texttt{column\_spec}, you can define specifications for
rows. Currently, you can either bold or italicize an entire row. Note
that, similar to other row-related functions in \texttt{kableExtra}, for
the position of the target row, you don't need to count in header rows
or the group labeling rows.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\StringTok{"striped"}\NormalTok{, }\DataTypeTok{full_width =}\NormalTok{ F) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{7}\NormalTok{, }\DataTypeTok{border_left =}\NormalTok{ T, }\DataTypeTok{bold =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{row_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{strikeout =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{row_spec}\NormalTok{(}\DecValTok{3}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{color =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{background =} \StringTok{"black"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering
\begin{tabular}{lrrrrr|>{\bfseries}r}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
\sout{Mazda RX4} & \sout{21.0} & \sout{6} & \sout{160} & \sout{110} & \sout{3.90} & \sout{2.620}\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
\rowcolor{black} \textcolor{white}{\textbf{Datsun 710}} & \textcolor{white}{\textbf{22.8}} & \textcolor{white}{\textbf{4}} & \textcolor{white}{\textbf{108}} & \textcolor{white}{\textbf{93}} & \textcolor{white}{\textbf{3.85}} & \textcolor{white}{\textbf{2.320}}\\
\rowcolor{black} \textcolor{white}{\textbf{Hornet 4 Drive}} & \textcolor{white}{\textbf{21.4}} & \textcolor{white}{\textbf{6}} & \textcolor{white}{\textbf{258}} & \textcolor{white}{\textbf{110}} & \textcolor{white}{\textbf{3.08}} & \textcolor{white}{\textbf{3.215}}\\
\rowcolor{black} \textcolor{white}{\textbf{Hornet Sportabout}} & \textcolor{white}{\textbf{18.7}} & \textcolor{white}{\textbf{8}} & \textcolor{white}{\textbf{360}} & \textcolor{white}{\textbf{175}} & \textcolor{white}{\textbf{3.15}} & \textcolor{white}{\textbf{3.440}}\\
\bottomrule
\end{tabular}
\end{table}
\hypertarget{header-rows}{%
\subsection{Header Rows}\label{header-rows}}
One special case of \texttt{row\_spec} is that you can specify the
format of the header row via \texttt{row\_spec(row\ =\ 0,\ ...)}.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \StringTok{"striped"}\NormalTok{, }\DataTypeTok{full_width =}\NormalTok{ F) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{row_spec}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DataTypeTok{angle =} \DecValTok{45}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering\rowcolors{2}{gray!6}{white}
\begin{tabular}{lcccccc}
\hiderowcolors
\toprule
\rotatebox{45}{ } & \rotatebox{45}{mpg} & \rotatebox{45}{cyl} & \rotatebox{45}{disp} & \rotatebox{45}{hp} & \rotatebox{45}{drat} & \rotatebox{45}{wt}\\
\midrule
\showrowcolors
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
\rowcolors{2}{white}{white}
\end{table}
\hypertarget{celltext-specification}{%
\section{Cell/Text Specification}\label{celltext-specification}}
Function \texttt{cell\_spec} is introduced in version 0.6.0 of
\texttt{kableExtra}. Unlike \texttt{column\_spec} and
\texttt{row\_spec}, \textbf{this function is designed to be used before
the data.frame gets into the \texttt{kable} function}. Comparing with
figuring out a list of 2 dimensional indexes for targeted cells, this
design is way easier to learn and use, and it fits perfectly well with
\texttt{dplyr}'s \texttt{mutate} and \texttt{summarize} functions. With
this design, there are two things to be noted: * Since
\texttt{cell\_spec} generates raw \texttt{HTML} or \texttt{LaTeX} code,
make sure you remember to put \texttt{escape\ =\ FALSE} in
\texttt{kable}. At the same time, you have to escape special symbols
including \texttt{\%} manually by yourself * \texttt{cell\_spec} needs a
way to know whether you want \texttt{html} or \texttt{latex}. You can
specify it locally in function or globally via the
\texttt{options(knitr.table.format\ =\ "latex")} method as suggested at
the beginning. If you don't provide anything, this function will output
as HTML by default.
Currently, \texttt{cell\_spec} supports features including bold, italic,
monospace, text color, background color, align, font size \& rotation
angle. More features may be added in the future. Please see function
documentations as reference.
\hypertarget{conditional-logic}{%
\subsection{Conditional logic}\label{conditional-logic}}
It is very easy to use \texttt{cell\_spec} with conditional logic. Here
is an example.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(dplyr)}
\NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{2}\NormalTok{] }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{mutate}\NormalTok{(}
\DataTypeTok{car =} \KeywordTok{row.names}\NormalTok{(.),}
\CommentTok{# You don't need format = "latex" if you have ever defined options(knitr.table.format)}
\DataTypeTok{mpg =} \KeywordTok{cell_spec}\NormalTok{(mpg, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \KeywordTok{ifelse}\NormalTok{(mpg }\OperatorTok{>}\StringTok{ }\DecValTok{20}\NormalTok{, }\StringTok{"red"}\NormalTok{, }\StringTok{"blue"}\NormalTok{)),}
\DataTypeTok{cyl =} \KeywordTok{cell_spec}\NormalTok{(cyl, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{angle =} \DecValTok{45}\NormalTok{, }
\DataTypeTok{background =} \KeywordTok{factor}\NormalTok{(cyl, }\KeywordTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\DecValTok{6}\NormalTok{, }\DecValTok{8}\NormalTok{), }
\KeywordTok{c}\NormalTok{(}\StringTok{"#666666"}\NormalTok{, }\StringTok{"#999999"}\NormalTok{, }\StringTok{"#BBBBBB"}\NormalTok{)))}
\NormalTok{ ) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{select}\NormalTok{(car, mpg, cyl) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable}\NormalTok{(}\StringTok{"latex"}\NormalTok{, }\DataTypeTok{escape =}\NormalTok{ F, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{linesep =} \StringTok{""}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{lll}
\toprule
car & mpg & cyl\\
\midrule
Mazda RX4 & \textcolor{red}{21} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{999999}{\textcolor{white}{6}}}}\\
Mazda RX4 Wag & \textcolor{red}{21} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{999999}{\textcolor{white}{6}}}}\\
Datsun 710 & \textcolor{red}{22.8} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{666666}{\textcolor{white}{4}}}}\\
Hornet 4 Drive & \textcolor{red}{21.4} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{999999}{\textcolor{white}{6}}}}\\
Hornet Sportabout & \textcolor{blue}{18.7} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{BBBBBB}{\textcolor{white}{8}}}}\\
Valiant & \textcolor{blue}{18.1} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{999999}{\textcolor{white}{6}}}}\\
Duster 360 & \textcolor{blue}{14.3} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{BBBBBB}{\textcolor{white}{8}}}}\\
Merc 240D & \textcolor{red}{24.4} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{666666}{\textcolor{white}{4}}}}\\
Merc 230 & \textcolor{red}{22.8} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{666666}{\textcolor{white}{4}}}}\\
Merc 280 & \textcolor{blue}{19.2} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{999999}{\textcolor{white}{6}}}}\\
\bottomrule
\end{tabular}
\hypertarget{visualize-data-with-viridis-color}{%
\subsection{Visualize data with Viridis
Color}\label{visualize-data-with-viridis-color}}
This package also comes with a few helper functions, including
\texttt{spec\_color}, \texttt{spec\_font\_size} \& \texttt{spec\_angle}.
These functions can rescale continuous variables to certain scales. For
example, function \texttt{spec\_color} would map a continuous variable
to any \href{https://CRAN.R-project.org/package=viridisLite}{viridis
color palettes}. It offers a very visually impactful representation in a
tabular format.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{iris[}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, ] }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{mutate_if}\NormalTok{(is.numeric, }\ControlFlowTok{function}\NormalTok{(x) \{}
\KeywordTok{cell_spec}\NormalTok{(x, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{color =} \KeywordTok{spec_color}\NormalTok{(x, }\DataTypeTok{end =} \FloatTok{0.9}\NormalTok{),}
\DataTypeTok{font_size =} \KeywordTok{spec_font_size}\NormalTok{(x))}
\NormalTok{ \}) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{Species =} \KeywordTok{cell_spec}\NormalTok{(}
\NormalTok{ Species, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T,}
\DataTypeTok{background =} \KeywordTok{spec_color}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DataTypeTok{end =} \FloatTok{0.9}\NormalTok{, }\DataTypeTok{option =} \StringTok{"A"}\NormalTok{, }\DataTypeTok{direction =} \DecValTok{-1}\NormalTok{)}
\NormalTok{ )) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable}\NormalTok{(}\StringTok{"latex"}\NormalTok{, }\DataTypeTok{escape =}\NormalTok{ F, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{linesep =} \StringTok{""}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{ccccc}
\toprule
Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
\midrule
\bgroup\fontsize{14}{16}\selectfont \textcolor[HTML]{28AE80}{\textbf{5.1}}\egroup{} & \bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{1F9A8A}{\textbf{3.5}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{3E4B8A}{\textbf{1.4}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{FECE91}{\textcolor{white}{\textbf{setosa}}}\\
\bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{4.9}}\egroup{} & \bgroup\fontsize{9}{11}\selectfont \textcolor[HTML]{482274}{\textbf{3}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{3E4B8A}{\textbf{1.4}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{FEA06D}{\textcolor{white}{\textbf{setosa}}}\\
\bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{39578C}{\textbf{4.7}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{38588C}{\textbf{3.2}}\egroup{} & \bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{440154}{\textbf{1.3}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{F66E5C}{\textcolor{white}{\textbf{setosa}}}\\
\bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{433E85}{\textbf{4.6}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{433E85}{\textbf{3.1}}\egroup{} & \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{1.5}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{DE4968}{\textcolor{white}{\textbf{setosa}}}\\
\bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{1F9A8A}{\textbf{5}}\egroup{} & \bgroup\fontsize{14}{16}\selectfont \textcolor[HTML]{29AF7F}{\textbf{3.6}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{3E4B8A}{\textbf{1.4}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{B73779}{\textcolor{white}{\textbf{setosa}}}\\
\bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{BBDF27}{\textbf{5.4}}\egroup{} & \bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{BBDF27}{\textbf{3.9}}\egroup{} & \bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{BBDF27}{\textbf{1.7}}\egroup{} & \bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{BBDF27}{\textbf{0.4}}\egroup{} & \cellcolor[HTML]{8C2981}{\textcolor{white}{\textbf{setosa}}}\\
\bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{433E85}{\textbf{4.6}}\egroup{} & \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{3.4}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{3E4B8A}{\textbf{1.4}}\egroup{} & \bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{22A884}{\textbf{0.3}}\egroup{} & \cellcolor[HTML]{641A80}{\textcolor{white}{\textbf{setosa}}}\\
\bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{1F9A8A}{\textbf{5}}\egroup{} & \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{3.4}}\egroup{} & \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{1.5}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{3C0F70}{\textcolor{white}{\textbf{setosa}}}\\
\bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{440154}{\textbf{4.4}}\egroup{} & \bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{440154}{\textbf{2.9}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{3E4B8A}{\textbf{1.4}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{140E36}{\textcolor{white}{\textbf{setosa}}}\\
\bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{4.9}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{433E85}{\textbf{3.1}}\egroup{} & \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{1.5}}\egroup{} & \bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{440154}{\textbf{0.1}}\egroup{} & \cellcolor[HTML]{000004}{\textcolor{white}{\textbf{setosa}}}\\
\bottomrule
\end{tabular}
In the example above, I'm using the \texttt{mutate} functions from
\texttt{dplyr}. You don't have to use it. Base R solutions like
\texttt{iris\$Species\ \textless{}-\ cell\_spec(iris\$Species,\ color\ =\ "red")}
also works.
\hypertarget{text-specification}{%
\subsection{Text Specification}\label{text-specification}}
If you check the results of \texttt{cell\_spec}, you will find that this
function does nothing more than wrapping the text with appropriate
HTML/LaTeX formatting syntax. The result of this function is just a
vector of character strings. As a result, when you are writing a
\texttt{rmarkdown} document or write some text in shiny apps, if you
need extra markups other than \textbf{bold} or \emph{italic}, you may
use this function to \textcolor{red}{color},
\bgroup\fontsize{16}{18}\selectfont change font size \egroup{} or
\rotatebox{30}{rotate} your text.
An aliased function \texttt{text\_spec} is also provided for a more
literal writing experience. The only difference is that in LaTeX, unless
you specify \texttt{latex\_background\_in\_cell\ =\ FALSE} (default is
\texttt{TRUE}) in \texttt{cell\_spec}, it will define cell background
color as \texttt{\textbackslash{}cellcolor\{\}}, which doesn't work
outside of a table, while for \texttt{text\_spec}, the default value for
\texttt{latex\_background\_in\_cell} is \texttt{FALSE}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{sometext <-}\StringTok{ }\KeywordTok{strsplit}\NormalTok{(}\KeywordTok{paste0}\NormalTok{(}
\StringTok{"You can even try to make some crazy things like this paragraph. "}\NormalTok{, }
\StringTok{"It may seem like a useless feature right now but it's so cool "}\NormalTok{,}
\StringTok{"and nobody can resist. ;)"}
\NormalTok{), }\StringTok{" "}\NormalTok{)[[}\DecValTok{1}\NormalTok{]]}
\NormalTok{text_formatted <-}\StringTok{ }\KeywordTok{paste}\NormalTok{(}
\KeywordTok{text_spec}\NormalTok{(sometext, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \KeywordTok{spec_color}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\KeywordTok{length}\NormalTok{(sometext), }\DataTypeTok{end =} \FloatTok{0.9}\NormalTok{),}
\DataTypeTok{font_size =} \KeywordTok{spec_font_size}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\KeywordTok{length}\NormalTok{(sometext), }\DataTypeTok{begin =} \DecValTok{5}\NormalTok{, }\DataTypeTok{end =} \DecValTok{20}\NormalTok{)),}
\DataTypeTok{collapse =} \StringTok{" "}\NormalTok{)}
\CommentTok{# To display the text, type `r text_formatted` outside of the chunk}
\end{Highlighting}
\end{Shaded}
\bgroup\fontsize{5}{7}\selectfont \textcolor[HTML]{440154}{You}\egroup{} \bgroup\fontsize{6}{8}\selectfont \textcolor[HTML]{470D60}{can}\egroup{} \bgroup\fontsize{6}{8}\selectfont \textcolor[HTML]{48186A}{even}\egroup{} \bgroup\fontsize{7}{9}\selectfont \textcolor[HTML]{482274}{try}\egroup{} \bgroup\fontsize{7}{9}\selectfont \textcolor[HTML]{472D7A}{to}\egroup{} \bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{453681}{make}\egroup{} \bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{424086}{some}\egroup{} \bgroup\fontsize{9}{11}\selectfont \textcolor[HTML]{3E4989}{crazy}\egroup{} \bgroup\fontsize{9}{11}\selectfont \textcolor[HTML]{3B518B}{things}\egroup{} \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{375A8C}{like}\egroup{} \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{33628D}{this}\egroup{} \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{306A8E}{paragraph.}\egroup{} \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{2C718E}{It}\egroup{} \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{29798E}{may}\egroup{} \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{26818E}{seem}\egroup{} \bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{23888E}{like}\egroup{} \bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{21908D}{a}\egroup{} \bgroup\fontsize{14}{16}\selectfont \textcolor[HTML]{1F968B}{useless}\egroup{} \bgroup\fontsize{14}{16}\selectfont \textcolor[HTML]{1F9E89}{feature}\egroup{} \bgroup\fontsize{15}{17}\selectfont \textcolor[HTML]{21A585}{right}\egroup{} \bgroup\fontsize{15}{17}\selectfont \textcolor[HTML]{26AD81}{now}\egroup{} \bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{30B47C}{but}\egroup{} \bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{3BBB75}{it's}\egroup{} \bgroup\fontsize{17}{19}\selectfont \textcolor[HTML]{4AC16D}{so}\egroup{} \bgroup\fontsize{17}{19}\selectfont \textcolor[HTML]{5AC864}{cool}\egroup{} \bgroup\fontsize{18}{20}\selectfont \textcolor[HTML]{6CCD5A}{and}\egroup{} \bgroup\fontsize{18}{20}\selectfont \textcolor[HTML]{7FD34E}{nobody}\egroup{} \bgroup\fontsize{19}{21}\selectfont \textcolor[HTML]{91D742}{can}\egroup{} \bgroup\fontsize{19}{21}\selectfont \textcolor[HTML]{A6DB35}{resist.}\egroup{} \bgroup\fontsize{20}{22}\selectfont \textcolor[HTML]{BBDF27}{;)}\egroup{}
\hypertarget{grouped-columns-rows}{%
\section{Grouped Columns / Rows}\label{grouped-columns-rows}}
\hypertarget{add-header-rows-to-group-columns}{%
\subsection{Add header rows to group
columns}\label{add-header-rows-to-group-columns}}
Tables with multi-row headers can be very useful to demonstrate grouped
data. To do that, you can pipe your kable object into
\texttt{add\_header\_above()}. The header variable is supposed to be a
named character with the names as new column names and values as column
span. For your convenience, if column span equals to 1, you can ignore
the \texttt{=1} part so the function below can be written as
`add\_header\_above(c(" ``,''Group 1" = 2, ``Group 2'' = 2, ``Group 3''
= 2)).
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{() }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{ =}\StringTok{ }\DecValTok{1}\NormalTok{, }\StringTok{"Group 1"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{, }\StringTok{"Group 2"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{, }\StringTok{"Group 3"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering
\begin{tabular}{lrrrrrr}
\toprule
\multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group 1} & \multicolumn{2}{c}{Group 2} & \multicolumn{2}{c}{Group 3} \\
\cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-5} \cmidrule(l{3pt}r{3pt}){6-7}
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
\end{table}
In fact, if you want to add another row of header on top, please feel
free to do so. Also, since kableExtra 0.3.0, you can specify
\texttt{bold} \& \texttt{italic} as you do in \texttt{row\_spec()}.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \StringTok{"striped"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group 1"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{, }\StringTok{"Group 2"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{, }\StringTok{"Group 3"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{)) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group 4"}\NormalTok{ =}\StringTok{ }\DecValTok{4}\NormalTok{, }\StringTok{"Group 5"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{)) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group 6"}\NormalTok{ =}\StringTok{ }\DecValTok{6}\NormalTok{), }\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{italic =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering\rowcolors{2}{gray!6}{white}
\begin{tabular}{lrrrrrr}
\hiderowcolors
\toprule
\multicolumn{1}{c}{\em{\textbf{ }}} & \multicolumn{6}{c}{\em{\textbf{Group 6}}} \\
\cmidrule(l{3pt}r{3pt}){2-7}
\multicolumn{1}{c}{ } & \multicolumn{4}{c}{Group 4} & \multicolumn{2}{c}{Group 5} \\
\cmidrule(l{3pt}r{3pt}){2-5} \cmidrule(l{3pt}r{3pt}){6-7}
\multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group 1} & \multicolumn{2}{c}{Group 2} & \multicolumn{2}{c}{Group 3} \\
\cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-5} \cmidrule(l{3pt}r{3pt}){6-7}
& mpg & cyl & disp & hp & drat & wt\\
\midrule
\showrowcolors
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
\rowcolors{2}{white}{white}
\end{table}
\hypertarget{group-rows-via-labeling}{%
\subsection{Group rows via labeling}\label{group-rows-via-labeling}}
Sometimes we want a few rows of the table being grouped together. They
might be items under the same topic (e.g., animals in one species) or
just different data groups for a categorical variable (e.g., age
\textless{} 40, age \textgreater{} 40). With the new function
\texttt{group\_rows()} in \texttt{kableExtra}, this kind of task can be
completed in one line. Please see the example below. Note that when you
count for the start/end rows of the group, you don't need to count for
the header rows nor other group label rows. You only need to think about
the row numbers in the ``original R dataframe''.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{6}\NormalTok{], }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{caption =} \StringTok{"Group Rows"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{() }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{group_rows}\NormalTok{(}\StringTok{"Group 1"}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{7}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{group_rows}\NormalTok{(}\StringTok{"Group 2"}\NormalTok{, }\DecValTok{8}\NormalTok{, }\DecValTok{10}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{table}[t]
\caption{\label{tab:unnamed-chunk-24}Group Rows}
\centering
\begin{tabular}{lrrrrrr}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108.0 & 93 & 3.85 & 2.320\\
\addlinespace[0.3em]
\multicolumn{7}{l}{\textbf{Group 1}}\\
\hspace{1em}Hornet 4 Drive & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.215\\
\hspace{1em}Hornet Sportabout & 18.7 & 8 & 360.0 & 175 & 3.15 & 3.440\\
\hspace{1em}Valiant & 18.1 & 6 & 225.0 & 105 & 2.76 & 3.460\\
\hspace{1em}Duster 360 & 14.3 & 8 & 360.0 & 245 & 3.21 & 3.570\\
\addlinespace[0.3em]
\multicolumn{7}{l}{\textbf{Group 2}}\\
\hspace{1em}Merc 240D & 24.4 & 4 & 146.7 & 62 & 3.69 & 3.190\\
\hspace{1em}Merc 230 & 22.8 & 4 & 140.8 & 95 & 3.92 & 3.150\\
\hspace{1em}Merc 280 & 19.2 & 6 & 167.6 & 123 & 3.92 & 3.440\\
\bottomrule
\end{tabular}
\end{table}
In case some users need it, you can define your own gapping spaces
between the group labeling row and previous rows. The default value is
\texttt{0.5em}.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{group_rows}\NormalTok{(}\StringTok{"Group 1"}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{, }\DataTypeTok{latex_gap_space =} \StringTok{"2em"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{lrrrrrr}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
\addlinespace[2em]
\multicolumn{7}{l}{\textbf{Group 1}}\\
\hspace{1em}Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
\hspace{1em}Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
If you prefer to build multiple groups in one step, you can use the
short-hand \texttt{index} option. Basically, you can use it in the same
way as you use \texttt{add\_header\_above}. However, since
\texttt{group\_row} only support one layer of grouping, you can't add
multiple layers of grouping header as you can do in
\texttt{add\_header\_above}.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{6}\NormalTok{], }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{caption =} \StringTok{"Group Rows"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{() }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{group_rows}\NormalTok{(}\DataTypeTok{index=}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{ =}\StringTok{ }\DecValTok{3}\NormalTok{, }\StringTok{"Group 1"}\NormalTok{ =}\StringTok{ }\DecValTok{4}\NormalTok{, }\StringTok{"Group 2"}\NormalTok{ =}\StringTok{ }\DecValTok{3}\NormalTok{))}
\CommentTok{# Not evaluated. The code above should have the same result as the first example in this section.}
\end{Highlighting}
\end{Shaded}
Note that \texttt{kable} has a relatively special feature to handle
\texttt{align} and it may bring troubles to you if you are not using it
correctly. In the documentation of the \texttt{align} argument of
\texttt{kable}, it says:
\begin{quote}
If \texttt{length(align)\ ==\ 1L}, the string will be expanded to a
vector of individual letters, e.g.
\texttt{\textquotesingle{}clc\textquotesingle{}} becomes
\texttt{c(\textquotesingle{}c\textquotesingle{},\ \textquotesingle{}l\textquotesingle{},\ \textquotesingle{}c\textquotesingle{})},
\textbf{unless the output format is LaTeX}.
\end{quote}
For example,
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{2}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{2}\NormalTok{], }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \KeywordTok{c}\NormalTok{(}\StringTok{"cl"}\NormalTok{))}
\CommentTok{# \textbackslash{}begin\{tabular\}\{l|cl|cl\} # Note the column alignment here}
\CommentTok{# \textbackslash{}hline}
\CommentTok{# & mpg & cyl\textbackslash{}\textbackslash{}}
\CommentTok{# ...}
\end{Highlighting}
\end{Shaded}
LaTeX, somehow shows surprisingly high tolerance on that, which is quite
unusual. As a result, it won't throw an error if you are just using
\texttt{kable} to make some simple tables. However, when you use
\texttt{kableExtra} to make some advanced modification, it will start to
throw some bugs. As a result, please try to form a habit of using a
vector in the \texttt{align} argument for \texttt{kable} (tip: you can
use \texttt{rep} function to replicate elements. For example,
\texttt{c("c",\ rep("l",\ 10))}).
\hypertarget{row-indentation}{%
\subsection{Row indentation}\label{row-indentation}}
Unlike \texttt{group\_rows()}, which will insert a labeling row,
sometimes we want to list a few sub groups under a total one. In that
case, \texttt{add\_indent()} is probably more appropriate.\\
For advanced users, you can even define your own css for the group
labeling.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{add_indent}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{5}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{lrrrrrr}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
\hspace{1em}Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
\hspace{1em}Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
\hspace{1em}Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
\hypertarget{group-rows-via-multi-row-cell}{%
\subsection{Group rows via multi-row
cell}\label{group-rows-via-multi-row-cell}}
Function \texttt{group\_rows} is great for showing simple structural
information on rows but sometimes people may need to show structural
information with multiple layers. When it happens, you may consider
using \texttt{collapse\_rows} instead, which will put repeating cells in
columns into multi-row cells.
In LaTeX, \texttt{collapse\_rows} adds some extra hlines to help
differentiate groups. You can customize this behavior using the
\texttt{latex\_hline} argument. You can choose from \texttt{full}
(default), \texttt{major} and \texttt{none}. Vertical alignment of cells
is controlled by the \texttt{valign} option. You can choose from
``top'', ``middle''(default) and ``bottom''. Be cautious that the
vertical alignment option was only introduced in multirow in 2016. If
you are using a legacy LaTeX distribution, you will run into trouble if
you set \texttt{valign} to be either ``top'' or ``bottom''.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{collapse_rows_dt <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{C1 =} \KeywordTok{c}\NormalTok{(}\KeywordTok{rep}\NormalTok{(}\StringTok{"a"}\NormalTok{, }\DecValTok{10}\NormalTok{), }\KeywordTok{rep}\NormalTok{(}\StringTok{"b"}\NormalTok{, }\DecValTok{5}\NormalTok{)),}
\DataTypeTok{C2 =} \KeywordTok{c}\NormalTok{(}\KeywordTok{rep}\NormalTok{(}\StringTok{"c"}\NormalTok{, }\DecValTok{7}\NormalTok{), }\KeywordTok{rep}\NormalTok{(}\StringTok{"d"}\NormalTok{, }\DecValTok{3}\NormalTok{), }\KeywordTok{rep}\NormalTok{(}\StringTok{"c"}\NormalTok{, }\DecValTok{2}\NormalTok{), }\KeywordTok{rep}\NormalTok{(}\StringTok{"d"}\NormalTok{, }\DecValTok{3}\NormalTok{)),}
\DataTypeTok{C3 =} \DecValTok{1}\OperatorTok{:}\DecValTok{15}\NormalTok{,}
\DataTypeTok{C4 =} \KeywordTok{sample}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{1}\NormalTok{), }\DecValTok{15}\NormalTok{, }\DataTypeTok{replace =} \OtherTok{TRUE}\NormalTok{))}
\KeywordTok{kable}\NormalTok{(collapse_rows_dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold=}\NormalTok{T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{collapse_rows}\NormalTok{(}\DataTypeTok{columns =} \DecValTok{1}\OperatorTok{:}\DecValTok{2}\NormalTok{, }\DataTypeTok{latex_hline =} \StringTok{"major"}\NormalTok{, }\DataTypeTok{valign =} \StringTok{"middle"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{>{\bfseries}cccc}
\toprule
C1 & C2 & C3 & C4\\
\midrule
& & 1 & 1\\
& & 2 & 0\\
& & 3 & 0\\
& & 4 & 1\\
& & 5 & 1\\
& & 6 & 1\\
& \multirow{-7}{*}{\centering\arraybackslash c} & 7 & 1\\
& & 8 & 0\\
& & 9 & 0\\
\multirow{-10}{*}{\centering\arraybackslash a} & \multirow{-3}{*}{\centering\arraybackslash d} & 10 & 1\\
\cmidrule{1-4}
& & 11 & 0\\
& \multirow{-2}{*}{\centering\arraybackslash c} & 12 & 1\\
& & 13 & 0\\
& & 14 & 1\\
\multirow{-5}{*}{\centering\arraybackslash b} & \multirow{-3}{*}{\centering\arraybackslash d} & 15 & 0\\
\bottomrule
\end{tabular}
Right now, you can't automatically make striped rows based on collapsed
rows but you can do it manually via the \texttt{extra\_latex\_after}
option in \texttt{row\_spec}. This feature is not officially supported.
I'm only document it here if you want to give it a try.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(collapse_rows_dt[}\OperatorTok{-}\DecValTok{1}\NormalTok{], }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{width =} \StringTok{"5em"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{row_spec}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{7}\NormalTok{, }\DecValTok{11}\OperatorTok{:}\DecValTok{12}\NormalTok{) }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{, }\DataTypeTok{extra_latex_after =} \StringTok{"}\CharTok{\textbackslash{}\textbackslash{}}\StringTok{rowcolor\{gray!6\}"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{collapse_rows}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{latex_hline =} \StringTok{"none"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{>{\bfseries\centering\arraybackslash}p{5em}cc}
\toprule
C2 & C3 & C4\\
\rowcolor{gray!6}
\midrule
& 1 & 1\\
\rowcolor{gray!6}
& 2 & 0\\
\rowcolor{gray!6}
& 3 & 0\\
\rowcolor{gray!6}
& 4 & 1\\
\rowcolor{gray!6}
& 5 & 1\\
\rowcolor{gray!6}
& 6 & 1\\
\rowcolor{gray!6}
\multirow{-7}{5em}{\centering\arraybackslash c} & 7 & 1\\
& 8 & 0\\
& 9 & 0\\
\multirow{-3}{5em}{\centering\arraybackslash d} & 10 & 1\\
\rowcolor{gray!6}
& 11 & 0\\
\rowcolor{gray!6}
\multirow{-2}{5em}{\centering\arraybackslash c} & 12 & 1\\
& 13 & 0\\
& 14 & 1\\
\multirow{-3}{5em}{\centering\arraybackslash d} & 15 & 0\\
\bottomrule
\end{tabular}
When there are too many layers, sometimes the table can become too wide.
You can choose to stack the first few layers by setting
\texttt{row\_group\_label\_position} to \texttt{stack}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{collapse_rows_dt <-}\StringTok{ }\KeywordTok{expand.grid}\NormalTok{(}
\DataTypeTok{Country =} \KeywordTok{sprintf}\NormalTok{(}\StringTok{'Country with a long name %s'}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\StringTok{'A'}\NormalTok{, }\StringTok{'B'}\NormalTok{)),}
\DataTypeTok{State =} \KeywordTok{sprintf}\NormalTok{(}\StringTok{'State %s'}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\StringTok{'a'}\NormalTok{, }\StringTok{'b'}\NormalTok{)),}
\DataTypeTok{City =} \KeywordTok{sprintf}\NormalTok{(}\StringTok{'City %s'}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\StringTok{'1'}\NormalTok{, }\StringTok{'2'}\NormalTok{)),}
\DataTypeTok{District =} \KeywordTok{sprintf}\NormalTok{(}\StringTok{'District %s'}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\StringTok{'1'}\NormalTok{, }\StringTok{'2'}\NormalTok{))}
\NormalTok{) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{arrange}\NormalTok{(Country, State, City) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{mutate_all}\NormalTok{(as.character) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{C1 =} \KeywordTok{rnorm}\NormalTok{(}\KeywordTok{n}\NormalTok{()),}
\DataTypeTok{C2 =} \KeywordTok{rnorm}\NormalTok{(}\KeywordTok{n}\NormalTok{()))}
\KeywordTok{kable}\NormalTok{(collapse_rows_dt, }\StringTok{"latex"}\NormalTok{, }
\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{linesep =} \StringTok{''}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{collapse_rows}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{3}\NormalTok{, }\DataTypeTok{row_group_label_position =} \StringTok{'stack'}\NormalTok{) }
\end{Highlighting}
\end{Shaded}
\begin{tabular}{cccccc}
\toprule
& & City & District & C1 & C2\\
\midrule
\addlinespace[0.3em]
\multicolumn{6}{l}{\textbf{Country with a long name A}}\\
\addlinespace[0.3em]
\multicolumn{6}{l}{\textit{State a}}\\
\hspace{1em}\hspace{1em} & & City 1 & District 1 & -0.1770795 & -0.2171630\\
\cmidrule{4-6}
\hspace{1em}\hspace{1em} & & & District 2 & -0.7480018 & -0.9850862\\
\cmidrule{3-6}
\hspace{1em}\hspace{1em} & & City 2 & District 1 & 0.7337258 & -0.5222169\\
\cmidrule{4-6}
\hspace{1em}\hspace{1em} & & & District 2 & -0.4759916 & -1.0309802\\
\cmidrule{2-6}
\addlinespace[0.3em]
\multicolumn{6}{l}{\textit{State b}}\\
\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.6750550 & -2.4739793\\
\cmidrule{4-6}
\hspace{1em}\hspace{1em} & & & District 2 & 0.3049046 & -1.2631447\\
\cmidrule{3-6}
\hspace{1em}\hspace{1em} & & City 2 & District 1 & 0.0286753 & 0.2100736\\
\cmidrule{4-6}
\hspace{1em}\hspace{1em} & & & District 2 & 0.5833306 & -0.1407191\\
\cmidrule{1-6}
\addlinespace[0.3em]
\multicolumn{6}{l}{\textbf{Country with a long name B}}\\
\addlinespace[0.3em]
\multicolumn{6}{l}{\textit{State a}}\\
\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.0431486 & -0.9681845\\
\cmidrule{4-6}
\hspace{1em}\hspace{1em} & & & District 2 & -0.6211451 & -1.4915492\\
\cmidrule{3-6}
\hspace{1em}\hspace{1em} & & City 2 & District 1 & -0.1115081 & 0.2991019\\
\cmidrule{4-6}
\hspace{1em}\hspace{1em} & & & District 2 & -1.1478388 & -1.4530953\\
\cmidrule{2-6}
\addlinespace[0.3em]
\multicolumn{6}{l}{\textit{State b}}\\
\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.1440275 & -0.5458611\\
\cmidrule{4-6}
\hspace{1em}\hspace{1em} & & & District 2 & 0.7375365 & 0.9307218\\
\cmidrule{3-6}
\hspace{1em}\hspace{1em} & & City 2 & District 1 & -0.2804654 & -0.6222302\\
\cmidrule{4-6}
\hspace{1em}\hspace{1em} & & & District 2 & 0.3877343 & 1.9276181\\
\bottomrule
\end{tabular}
To better distinguish different layers, you can format the each layer
using \texttt{row\_group\_label\_fonts}. You can also customize the
hlines to better differentiate groups.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{row_group_label_fonts <-}\StringTok{ }\KeywordTok{list}\NormalTok{(}
\KeywordTok{list}\NormalTok{(}\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{italic =}\NormalTok{ T), }
\KeywordTok{list}\NormalTok{(}\DataTypeTok{bold =}\NormalTok{ F, }\DataTypeTok{italic =}\NormalTok{ F)}
\NormalTok{ )}
\KeywordTok{kable}\NormalTok{(collapse_rows_dt, }\StringTok{"latex"}\NormalTok{, }
\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{linesep =} \StringTok{''}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold=}\NormalTok{T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{collapse_rows}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{3}\NormalTok{, }\DataTypeTok{latex_hline =} \StringTok{'custom'}\NormalTok{, }\DataTypeTok{custom_latex_hline =} \DecValTok{1}\OperatorTok{:}\DecValTok{3}\NormalTok{, }
\DataTypeTok{row_group_label_position =} \StringTok{'stack'}\NormalTok{, }
\DataTypeTok{row_group_label_fonts =}\NormalTok{ row_group_label_fonts) }
\end{Highlighting}
\end{Shaded}
\begin{tabular}{>{\bfseries}cccccc}
\toprule
& & City & District & C1 & C2\\
\midrule
\addlinespace[0.3em]
\multicolumn{6}{l}{\textit{\textbf{Country with a long name A}}}\\
\addlinespace[0.3em]
\multicolumn{6}{l}{State a}\\
\hspace{1em}\hspace{1em} & & City 1 & District 1 & -0.1770795 & -0.2171630\\
\hspace{1em}\hspace{1em} & & & District 2 & -0.7480018 & -0.9850862\\
\cmidrule{3-6}
\hspace{1em}\hspace{1em} & & City 2 & District 1 & 0.7337258 & -0.5222169\\
\hspace{1em}\hspace{1em} & & & District 2 & -0.4759916 & -1.0309802\\
\cmidrule{2-6}
\addlinespace[0.3em]
\multicolumn{6}{l}{State b}\\
\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.6750550 & -2.4739793\\
\hspace{1em}\hspace{1em} & & & District 2 & 0.3049046 & -1.2631447\\
\cmidrule{3-6}
\hspace{1em}\hspace{1em} & & City 2 & District 1 & 0.0286753 & 0.2100736\\
\hspace{1em}\hspace{1em} & & & District 2 & 0.5833306 & -0.1407191\\
\cmidrule{1-6}
\addlinespace[0.3em]
\multicolumn{6}{l}{\textit{\textbf{Country with a long name B}}}\\
\addlinespace[0.3em]
\multicolumn{6}{l}{State a}\\
\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.0431486 & -0.9681845\\
\hspace{1em}\hspace{1em} & & & District 2 & -0.6211451 & -1.4915492\\
\cmidrule{3-6}
\hspace{1em}\hspace{1em} & & City 2 & District 1 & -0.1115081 & 0.2991019\\
\hspace{1em}\hspace{1em} & & & District 2 & -1.1478388 & -1.4530953\\
\cmidrule{2-6}
\addlinespace[0.3em]
\multicolumn{6}{l}{State b}\\
\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.1440275 & -0.5458611\\
\hspace{1em}\hspace{1em} & & & District 2 & 0.7375365 & 0.9307218\\
\cmidrule{3-6}
\hspace{1em}\hspace{1em} & & City 2 & District 1 & -0.2804654 & -0.6222302\\
\hspace{1em}\hspace{1em} & & & District 2 & 0.3877343 & 1.9276181\\
\bottomrule
\end{tabular}
\hypertarget{table-footnote}{%
\section{Table Footnote}\label{table-footnote}}
\begin{quote}
Now it's recommended to use the new \texttt{footnote} function instead
of \texttt{add\_footnote} to make table footnotes.
\end{quote}
Documentations for \texttt{add\_footnote} can be found
\href{http://haozhu233.github.io/kableExtra/legacy_features\#add_footnote}{here}.
There are four notation systems in \texttt{footnote}, namely
\texttt{general}, \texttt{number}, \texttt{alphabet} and
\texttt{symbol}. The last three types of footnotes will be labeled with
corresponding marks while \texttt{general} won't be labeled. You can
pick any one of these systems or choose to display them all for
fulfilling the APA table footnotes requirements.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{full_width =}\NormalTok{ F) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{footnote}\NormalTok{(}\DataTypeTok{general =} \StringTok{"Here is a general comments of the table. "}\NormalTok{,}
\DataTypeTok{number =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote 1; "}\NormalTok{, }\StringTok{"Footnote 2; "}\NormalTok{),}
\DataTypeTok{alphabet =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote A; "}\NormalTok{, }\StringTok{"Footnote B; "}\NormalTok{),}
\DataTypeTok{symbol =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote Symbol 1; "}\NormalTok{, }\StringTok{"Footnote Symbol 2"}\NormalTok{)}
\NormalTok{ )}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering
\begin{tabular}{l|c|c|c|c|c|c}
\hline
& mpg & cyl & disp & hp & drat & wt\\
\hline
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
\hline
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
\hline
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
\hline
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
\hline
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\hline
\multicolumn{7}{l}{\textit{Note: }}\\
\multicolumn{7}{l}{Here is a general comments of the table. }\\
\multicolumn{7}{l}{\textsuperscript{1} Footnote 1; }\\
\multicolumn{7}{l}{\textsuperscript{2} Footnote 2; }\\
\multicolumn{7}{l}{\textsuperscript{a} Footnote A; }\\
\multicolumn{7}{l}{\textsuperscript{b} Footnote B; }\\
\multicolumn{7}{l}{\textsuperscript{*} Footnote Symbol 1; }\\
\multicolumn{7}{l}{\textsuperscript{\dag} Footnote Symbol 2}\\
\end{tabular}
\end{table}
You can also specify title for each category by using the
\texttt{***\_title} arguments. Default value for \texttt{general\_title}
is ``Note:'' and "" for the rest three. You can also change the order
using \texttt{footnote\_order}. You can even display footnote as chunk
texts (default is as a list) using \texttt{footnote\_as\_chunk}. The
font format of the titles are controlled by \texttt{title\_format} with
options including ``italic'' (default), ``bold'' and ``underline''.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{footnote}\NormalTok{(}\DataTypeTok{general =} \StringTok{"Here is a general comments of the table. "}\NormalTok{,}
\DataTypeTok{number =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote 1; "}\NormalTok{, }\StringTok{"Footnote 2; "}\NormalTok{),}
\DataTypeTok{alphabet =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote A; "}\NormalTok{, }\StringTok{"Footnote B; "}\NormalTok{),}
\DataTypeTok{symbol =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote Symbol 1; "}\NormalTok{, }\StringTok{"Footnote Symbol 2"}\NormalTok{),}
\DataTypeTok{general_title =} \StringTok{"General: "}\NormalTok{, }\DataTypeTok{number_title =} \StringTok{"Type I: "}\NormalTok{,}
\DataTypeTok{alphabet_title =} \StringTok{"Type II: "}\NormalTok{, }\DataTypeTok{symbol_title =} \StringTok{"Type III: "}\NormalTok{,}
\DataTypeTok{footnote_as_chunk =}\NormalTok{ T, }\DataTypeTok{title_format =} \KeywordTok{c}\NormalTok{(}\StringTok{"italic"}\NormalTok{, }\StringTok{"underline"}\NormalTok{)}
\NormalTok{ )}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{lcccccc}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\multicolumn{7}{l}{\underline{\textit{General: }} Here is a general comments of the table. }\\
\multicolumn{7}{l}{\underline{\textit{Type I: }} \textsuperscript{1} Footnote 1; \textsuperscript{2} Footnote 2; }\\
\multicolumn{7}{l}{\underline{\textit{Type II: }} \textsuperscript{a} Footnote A; \textsuperscript{b} Footnote B; }\\
\multicolumn{7}{l}{\underline{\textit{Type III: }} \textsuperscript{*} Footnote Symbol 1; \textsuperscript{\dag} Footnote Symbol 2}\\
\end{tabular}
If you need to add footnote marks in a table, you need to do it manually
(no fancy) using \texttt{footnote\_marker\_***()}. Remember that similar
with \texttt{cell\_spec}, you need to tell this function whether you
want it to do it in \texttt{HTML} (default) or \texttt{LaTeX}. You can
set it for all using the \texttt{knitr.table.format} global option.
Also, if you have ever used \texttt{footnote\_marker\_***()}, you need
to put \texttt{escape\ =\ F} in your \texttt{kable} function to avoid
escaping of special characters. Note that if you want to use these
\texttt{footnote\_marker} functions in \texttt{kableExtra} functions
like \texttt{group\_rows} (for the row label) or
\texttt{add\_header\_above}, you need to set
\texttt{double\_escape\ =\ T} and \texttt{escape\ =\ F} in those
functions. I'm trying to find other ways around. Please let me know if
you have a good idea and are willing to contribute.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{dt_footnote <-}\StringTok{ }\NormalTok{dt}
\KeywordTok{names}\NormalTok{(dt_footnote)[}\DecValTok{2}\NormalTok{] <-}\StringTok{ }\KeywordTok{paste0}\NormalTok{(}\KeywordTok{names}\NormalTok{(dt_footnote)[}\DecValTok{2}\NormalTok{], }
\CommentTok{# That "latex" can be eliminated if defined in global}
\KeywordTok{footnote_marker_symbol}\NormalTok{(}\DecValTok{1}\NormalTok{, }\StringTok{"latex"}\NormalTok{))}
\KeywordTok{row.names}\NormalTok{(dt_footnote)[}\DecValTok{4}\NormalTok{] <-}\StringTok{ }\KeywordTok{paste0}\NormalTok{(}\KeywordTok{row.names}\NormalTok{(dt_footnote)[}\DecValTok{4}\NormalTok{], }
\KeywordTok{footnote_marker_alphabet}\NormalTok{(}\DecValTok{1}\NormalTok{))}
\KeywordTok{kable}\NormalTok{(dt_footnote, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T,}
\CommentTok{# Remember this escape = F}
\DataTypeTok{escape =}\NormalTok{ F) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{footnote}\NormalTok{(}\DataTypeTok{alphabet =} \StringTok{"Footnote A; "}\NormalTok{,}
\DataTypeTok{symbol =} \StringTok{"Footnote Symbol 1; "}\NormalTok{,}
\DataTypeTok{alphabet_title =} \StringTok{"Type II: "}\NormalTok{, }\DataTypeTok{symbol_title =} \StringTok{"Type III: "}\NormalTok{,}
\DataTypeTok{footnote_as_chunk =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{lcccccc}
\toprule
& mpg & cyl\textsuperscript{*} & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive\textsuperscript{a} & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\multicolumn{7}{l}{\textit{Type II: } \textsuperscript{a} Footnote A; }\\
\multicolumn{7}{l}{\textit{Type III: } \textsuperscript{*} Footnote Symbol 1; }\\
\end{tabular}
If your table footnote is very long, please consider to put your table
in a \texttt{ThreePartTable} frame. Note that, in kableExtra version
\textless{}= 0.7.0, we were using \texttt{threeparttable} but since
kableExtra 0.8.0, we start to use \texttt{ThreePartTable} from
\texttt{threeparttablex} instead. \texttt{ThreePartTable} supports both
the \texttt{longtable} and \texttt{tabu} environments.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{caption =} \StringTok{"s"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{footnote}\NormalTok{(}\DataTypeTok{general =} \StringTok{"Here is a very very very very very very very very very very very very very very very very very very very very long footnote"}\NormalTok{, }
\DataTypeTok{threeparttable =}\NormalTok{ T)}
\end{Highlighting}
\end{Shaded}
\begin{table}[t]
\caption{\label{tab:unnamed-chunk-36}s}
\centering
\begin{threeparttable}
\begin{tabular}{lcccccc}
\toprule
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\end{tabular}
\begin{tablenotes}
\item \textit{Note: }
\item Here is a very very very very very very very very very very very very very very very very very very very very long footnote
\end{tablenotes}
\end{threeparttable}
\end{table}
\hypertarget{latex-only-features}{%
\section{LaTeX Only Features}\label{latex-only-features}}
\hypertarget{linebreak-processor}{%
\subsection{Linebreak processor}\label{linebreak-processor}}
Unlike in HTML, where you can use \texttt{\textless{}br\textgreater{}}
at any time, in LaTeX, it's actually quite difficult to make a linebreak
in a table. Therefore I created the \texttt{linebreak} function to
facilitate this process. Please see the
\href{http://haozhu233.github.io/kableExtra/best_practice_for_newline_in_latex_table.pdf}{Best
Practice for Newline in LaTeX Table} for details.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{dt_lb <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}
\DataTypeTok{Item =} \KeywordTok{c}\NormalTok{(}\StringTok{"Hello}\CharTok{\textbackslash{}n}\StringTok{World"}\NormalTok{, }\StringTok{"This}\CharTok{\textbackslash{}n}\StringTok{is a cat"}\NormalTok{), }
\DataTypeTok{Value =} \KeywordTok{c}\NormalTok{(}\DecValTok{10}\NormalTok{, }\DecValTok{100}\NormalTok{)}
\NormalTok{)}
\NormalTok{dt_lb }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{mutate_all}\NormalTok{(linebreak) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable}\NormalTok{(}\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{escape =}\NormalTok{ F,}
\DataTypeTok{col.names =} \KeywordTok{linebreak}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{"Item}\CharTok{\textbackslash{}n}\StringTok{(Name)"}\NormalTok{, }\StringTok{"Value}\CharTok{\textbackslash{}n}\StringTok{(Number)"}\NormalTok{), }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{))}
\end{Highlighting}
\end{Shaded}
\begin{tabular}{lr}
\toprule
\makecell[c]{Item\\(Name)} & \makecell[c]{Value\\(Number)}\\
\midrule
\makecell[l]{Hello\\World} & 10\\
\makecell[l]{This\\is a cat} & 100\\
\bottomrule
\end{tabular}
At the same time, since \texttt{kableExtra\ 0.8.0}, all
\texttt{kableExtra} functions that have some contents input (such as
\texttt{footnote} or \texttt{group\_rows}) will automatically convert
\texttt{\textbackslash{}n} to linebreaks for you in both LaTeX and HTML.
\hypertarget{table-on-a-landscape-page}{%
\subsection{Table on a Landscape Page}\label{table-on-a-landscape-page}}
Sometimes when we have a wide table, we want it to sit on a designated
landscape page. The new function \texttt{landscape()} can help you on
that. Unlike other functions, this little function only serves LaTeX and
doesn't have a HTML side.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{caption =} \StringTok{"Demo Table (Landscape)[note]"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"hold_position"}\NormalTok{)) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group 1[note]"}\NormalTok{ =}\StringTok{ }\DecValTok{3}\NormalTok{, }\StringTok{"Group 2[note]"}\NormalTok{ =}\StringTok{ }\DecValTok{3}\NormalTok{)) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{add_footnote}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{"This table is from mtcars"}\NormalTok{, }
\StringTok{"Group 1 contains mpg, cyl and disp"}\NormalTok{, }
\StringTok{"Group 2 contains hp, drat and wt"}\NormalTok{), }
\DataTypeTok{notation =} \StringTok{"symbol"}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{group_rows}\NormalTok{(}\StringTok{"Group 1"}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{landscape}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
\begin{landscape}\begin{table}[!h]
\caption{\label{tab:unnamed-chunk-38}Demo Table (Landscape)\textsuperscript{*}}
\centering
\begin{tabular}{lrrrrrr}
\toprule
\multicolumn{1}{c}{ } & \multicolumn{3}{c}{Group 1\textsuperscript{\dag}} & \multicolumn{3}{c}{Group 2\textsuperscript{\ddag}} \\
\cmidrule(l{3pt}r{3pt}){2-4} \cmidrule(l{3pt}r{3pt}){5-7}
& mpg & cyl & disp & hp & drat & wt\\
\midrule
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
\addlinespace[0.3em]
\multicolumn{7}{l}{\textbf{Group 1}}\\
\hspace{1em}Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
\hspace{1em}Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
\bottomrule
\multicolumn{7}{l}{\textsuperscript{*} This table is from mtcars}\\
\multicolumn{7}{l}{\textsuperscript{\dag} Group 1 contains mpg, cyl and disp}\\
\multicolumn{7}{l}{\textsuperscript{\ddag} Group 2 contains hp, drat and wt}\\
\end{tabular}
\end{table}
\end{landscape}
\hypertarget{use-latex-table-in-html-or-word}{%
\subsection{Use LaTeX table in HTML or
Word}\label{use-latex-table-in-html-or-word}}
If you want to include a LaTeX rendered table in your HTML or Word
document, or if you just want to save table as an image, you may
consider using \texttt{kable\_as\_image()}. Note that this feature
requires you to have \href{https://github.com/ropensci/magick}{magick}
installed (\texttt{install.packages("magick")}). Also, if you are
planning to use it on Windows, you need to install
\href{https://www.ghostscript.com/}{Ghostscript}. This feature may not
work if you are using tinytex. If you are using tinytex, please consider
using other alternatives to this function.
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Not evaluated. }
\CommentTok{# The code below will automatically include the image in the rmarkdown document}
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_as_image}\NormalTok{()}
\CommentTok{# If you want to save the image locally, just provide a name}
\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable_as_image}\NormalTok{(}\StringTok{"my_latex_table"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\hypertarget{from-other-packages}{%
\section{From other packages}\label{from-other-packages}}
Since the structure of \texttt{kable} is relatively simple, it shouldn't
be too difficult to convert HTML or LaTeX tables generated by other
packages to a \texttt{kable} object and then use \texttt{kableExtra} to
modify the outputs. If you are a package author, feel free to reach out
to me and we can collaborate.
\hypertarget{tables}{%
\subsection{\texorpdfstring{\texttt{tables}}{tables}}\label{tables}}
The latest version of
\href{https://CRAN.R-project.org/package=tables}{\texttt{tables}} comes
with a \texttt{toKable()} function, which is compatiable with functions
in \texttt{kableExtra} (\textgreater{}=0.9.0).
\end{document}