Example: bankruptcy

Create Awesome LaTeX Table with knitr::kable and kableExtra

Create Awesome LaTeX Table with knitr::kable and kableExtraHao Zhu2020-10-22 ContentsOverview3 Installation3 Getting Started3 LaTeX packages used in this package .. 4 Plain LaTeX .. 4 LaTeX Table with booktabs .. 5 Table Styles5 LaTeX options .. 5 Full width? .. 9 Position .. 9 Font Size .. 10 Column / Row Specification10 Column spec .. 10 Insert Images into Columns .. 12 Row spec .. 13 Header Rows .. 14 Cell/Text Specification14 Conditional logic .. 14 Visualize data with Viridis Color .. 15 Text Specification .. 16 Grouped Columns / Rows17 Add header rows to group columns .. 17 Group rows via labeling .. 18 Row indentation .. 19 Group rows via multi-row cell .. 19 Table Footnote23 LaTeX Only Features26 Linebreak processor .. 26 Table on a Landscape Page.

kbl(dt) mpg cyl disp hp drat wt MazdaRX4 21.0 6 160 110 3.90 2.620 MazdaRX4Wag 21.0 6 160 110 3.90 2.875 Datsun710 22.8 4 108 93 3.85 2.320 Hornet4Drive 21.4 6 258 110 3.08 3.215

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Create Awesome LaTeX Table with knitr::kable and kableExtra

1 Create Awesome LaTeX Table with knitr::kable and kableExtraHao Zhu2020-10-22 ContentsOverview3 Installation3 Getting Started3 LaTeX packages used in this package .. 4 Plain LaTeX .. 4 LaTeX Table with booktabs .. 5 Table Styles5 LaTeX options .. 5 Full width? .. 9 Position .. 9 Font Size .. 10 Column / Row Specification10 Column spec .. 10 Insert Images into Columns .. 12 Row spec .. 13 Header Rows .. 14 Cell/Text Specification14 Conditional logic .. 14 Visualize data with Viridis Color .. 15 Text Specification .. 16 Grouped Columns / Rows17 Add header rows to group columns .. 17 Group rows via labeling .. 18 Row indentation .. 19 Group rows via multi-row cell .. 19 Table Footnote23 LaTeX Only Features26 Linebreak processor .. 26 Table on a Landscape Page.

2 26 Decimal Alignment .. 28 Use LaTeX Table in HTML or Word .. 28 From other packages28tables.. 281xtable.. 292 Please see the package documentation site for how to use this package in HTML and goal ofkableExtrais to help you build common complex tables and manipulatetable styles. It imports the pipe%>%symbol frommagrittrand verbalizes all thefunctions, so basically you can add layers to a kable output in a way that is users who are not very familiar with the pipe operator%>%in R, it is the Rversion of the fluent interface. The ides is to pass the result along the chain for amore literal coding experience. Basically when we sayA %>% B, technically it meanssending the results of A to B as B s first learn how to generate complex tables in HTML, please visit (" kableExtra ")# For dev version# ("devtools")devtools::install_github("ha ozhu233/ kableExtra ")Getting StartedHere we are using the first few columns and rows from datasetmtcarslibrary( kableExtra )dt <- mtcars[1:5, 1:6]Key Update:In the latest version of this package ( +), we provide a wrapper funcitonkbltothe originalkablefunction with detailed documentation of all the hidden html/ LaTeX options.

3 Italso does auto-formatting check in every function call instead of relying on the global environementvariable. As a result, it also solves an issue for multi-format R Markdown documents. I encourageyou start to use the newkblfunction for all its convenience but the support for the originalkablefunction is still there. In this doc, we will usekblinstead paragraph is a little outdated. It s here only for education purpose because it s helpfulto understand howkableworks under the hood. When you are usingkable(), if you don t specifyformat, by default it will generate a markdown Table and let pandoc handle the conversion from markdownto HTML/PDF. This is the most favorable approach to render most simple tables as it is format you switch from HTML to pdf, you basically don t need to change anything in your code.

4 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, youshoulddefineformatinkable()as either html or LaTeX .You can also define a global option at thebeginning usingoptions( = "html")so you don t repeat the step , when you load this package (library( kableExtra )), it will automatically set upthe global option based on your current environment. Unless you are rendering a PDF,kableExtrawill try to render a HTML Table for no longer need to manually set either theglobal option or theformatoption in eachkable()function. I m still including the explanation abovehere in this vignette so you can understand what is going on behind the scene.

5 Note that this is only an globaloption. You can manually set any format inkable()whenever you want. I just hope you can enjoy a peace3of mind in most of your time. You can disable this behavior by settingoptions( FALSE)before you loadkableExtra.# If you are using kableExtra < , you are recommended to set a global option first.# options( = " LaTeX ")## If you don't define format here, you'll need put`format = " LaTeX "`## in every kable packages used in this packageIf you are using a recent version of rmarkdown, you are recommended to load this package entirely vialibrary( kableExtra )orrequire(kableExt ra)because this package will load all necessary LaTeX packages,such asbooktabsormultirow, for you automatically. Note that, if you are calling functions fromkableExtraviakableExtra::kable_styli ng()or if you putlibrary( kableExtra )in a separate R file that issourcedby the rmarkdown document, these packages won t be loaded.

6 Furthermore, you can suppress this auto-loading behavior by setting a global beFALSE before you loadkableExtra.# Not evaluated. Illustration purposeoptions( = FALSE)library( kableExtra )If you are using R Sweave, beamer, R package vignette template, tufte or some customized rmarkdowntemplates, you can put the following meta data into theyamlsection. If you are familar with LaTeX and youknow what you are doing, feel free to remove unnecessary packages from the :- \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}- \usepackage{xcolor}Note:kableExtrawas usingxcolorwith thetableoption for alternative row color before However, therecent updates infancyvbrcauses a clash inxcoloroption.

7 Therefore, we removed thexcolordependencyin version and started to rely reality, most cases, you still needxcolorto define new colors. The tricky part is that, if you are using aolder version ofrmarkdown,xcoloris not included in the template while in recent version, afterrmarkdownstarted to use the default pandoc template,xcoloris included by default. To minimize the effort, we choseto loadxcolorin the LaTeXPlain LaTeX Table looks relatively ugly in 2017.# Again, with kableExtra >= ,`format = " LaTeX "`is automatically defined# when this package gets loaded. Otherwise, you still need to define formats4kbl(dt)mpgcyldisphpdratwtMazda RX4 4 # Same: kable(dt, " LaTeX ") LaTeX Table with booktabsSimilar to Bootstrap in HTML, in LaTeX , you can also use a trick to make your Table look prettier as different part is that, this time you don t need to pipe kable outputs to another function.

8 Instead, youshould callbooktabs = Tdirectly inkable().kbl(dt, booktabs = T)mpg cyl disp hp dratwtMazda 6 160 110 RX4 6 160 110 4 108 93 4 6 258 110 Sportabout 8 360 175 Styleskable_stylingin LaTeX uses the same syntax and structure askable_stylingin HTML. However, insteadofbootstrap_options, you should optionsSimilar withbootstap_options,latex_optionsis also a charter vector with a bunch of options includingstriped, though in the LaTeX world, people usually call italternative row colorsbut here I m using itsbootstrap name for consistency. Note that to make it happen, LaTeX packagexcoloris required to beloaded. In an environment like rmarkdown::pdf_document (rmarkdown +),kable_stylingwill load itautomatically ifstripedis enabled.

9 However, in other cases, you probably need to import that package (dt, booktabs = T)%>%kable_styling(latex_options = "striped")5mpg cyl disp hp dratwtMazda RX4 6 160 110 4 6 258 110 can also specify which rows you want to striped on viastripe_index. In most case, you might wantto turn off the default 5 rows + a space setting inknitr::kable()by settinglinesep = "". See this SOanswer for details. (mtcars[1:8, 1:4], booktabs = T, linesep = "")%>%kable_styling(latex_options = "striped", stripe_index =c(1,2, 5:6))mpg cyl disp hpMazda RX4 4 93 Hornet 4 6 110 Hornet 8 245 Merc 4 62 Hold positionIf you provide a Table caption inkbl(), it will put your LaTeX tabular in atableenvironment, unless youare usinglongtable.

10 Atableenvironment will automatically find the best place (it thinks) to put yourtable. 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 thishold_positionoptions (dt, caption = "Demo Table ", booktabs = T)%>%kable_styling(latex_options =c("striped", "hold_position")) Table 1: Demo tablempg cyl disp hp dratwtMazda RX4 6 160 110 4 6 258 110 you findhold_positionis not powerful enough to literally PIN your Table in the exact position, you maywant to useHOLD_position, which is a more powerful version of this feature. For those who are familiarwith LaTeX ,hold_positionuses[!h]andHOLD_posit ionuses[H]and downWhen you have a wide Table that will normally go out of the page, and you want to scale down the tableto fit the page, you can use thescale_downoption here.


Related search queries