| ["index.html", "Using kableExtra in Bookdown Chapter 1 Introduction", " Using kableExtra in Bookdown Hao Zhu 2018-10-23 Chapter 1 Introduction If you have tried to use kableExtra in a bookdown project with mutltiple formats, you may have experienced some problems. For example, you might not be able to render HTML and PDF tables at the same time using the same piece of code. At the same time, you might start to wonder if it’s possible to create bootstrap style tables in gitbook. I will try to address these issues using this example. This book is hosted at https://haozhu233.github.io/kableExtra/bookdown/index.html You can download PDF & EPUB to see the results of kableExtra in those formats using the same code. You can find the source code for this project at https://github.com/haozhu233/kableExtra/tree/master/docs. "], |
| ["cross-format-tables-in-bookdown.html", "Chapter 2 Cross-format Tables in Bookdown 2.1 Use the “K-M” approach instead of “M-K” 2.2 Prepare Your Tables for All Formats", " Chapter 2 Cross-format Tables in Bookdown Note that if kableExtra 0.9.0 doesn’t support the EPUB format. If you need to output tables in .epub, please upgrade to the dev version or version 1.0 on CRAN once it’s released. 2.1 Use the “K-M” approach instead of “M-K” Please read this chapter about the “K-M”/“M-K” approaches in bookdown: https://bookdown.org/yihui/bookdown/new-session.html To generate cross-format tables with kableExtra in a multi-format bookdown project, you will have to use the “M-K” approach by setting new_session: true in _bookdown.yml. Somehow the “M-K” approach, which merges chapters to a big Rmd and then renders, shares the global environment across formats. For now, I’m not sure if this is a bug or intended behavior. It might be fixable in the future but please don’t count on that. By setting new_session: true, we force R to use a new session for every chapter for different formats. In this way, tables are generated differently in different formats. Note that the “M-K” approach is slower than the “K-M” approach. At the same time, packages and data are not shared accross chapter. # Example _bookdown.yml book_filename: "bookdown_example" delete_merged_file: true new_session: true language: ui: chapter_name: "Chapter " 2.2 Prepare Your Tables for All Formats In most cases, functions in kable and kableExtra use the same API to accomplish the same styling task in HTML and LaTeX. However, you also need some format specific settings so your tables will look good in both formats. Some common items here include the booktabs and longtable settings in kable and the bootstrap_options and latex_options in kable_styling. Here is an example for a table that will work in HTML, LaTeX & EPUB. library(kableExtra) library(dplyr) ## Warning: package 'dplyr' was built under R version 3.5.1 ## ## Attaching package: 'dplyr' ## The following objects are masked from 'package:stats': ## ## filter, lag ## The following objects are masked from 'package:base': ## ## intersect, setdiff, setequal, union options(kableExtra.html.bsTable = T) iris[1:10, ] %>% mutate_if(is.numeric, function(x) { cell_spec(x, bold = T, color = spec_color(x, end = 0.9), font_size = spec_font_size(x)) }) %>% mutate(Species = cell_spec( Species, color = "white", bold = T, background = spec_color(1:10, end = 0.9, option = "A", direction = -1) )) %>% kable(escape = F, align = "c", booktabs = T) %>% kable_styling(c("striped", "condensed"), latex_options = "striped", full_width = F) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 5.1 3.5 1.4 0.2 setosa 4.9 3 1.4 0.2 setosa 4.7 3.2 1.3 0.2 setosa 4.6 3.1 1.5 0.2 setosa 5 3.6 1.4 0.2 setosa 5.4 3.9 1.7 0.4 setosa 4.6 3.4 1.4 0.3 setosa 5 3.4 1.5 0.2 setosa 4.4 2.9 1.4 0.2 setosa 4.9 3.1 1.5 0.1 setosa "], |
| ["use-bootstrap-tables-in-gitbooks-epub.html", "Chapter 3 Use Bootstrap Tables in gitbooks & epub 3.1 Gitbook 3.2 Epub", " Chapter 3 Use Bootstrap Tables in gitbooks & epub 3.1 Gitbook Most of kableExtra tricks will work in bookdown except those requiring bootstrap. By default, rmarkdown won’t load bootstrap for you on gitbook as it’s not necesary. In kableExtra, I used the bootstrap 3.3.7 customization tool and made a customized css copy. You can load it by setting options(kableExtra.html.bsTable = T). library(kableExtra) options(kableExtra.html.bsTable = T) mtcars[1:5, 1:5] %>% kable(booktabs = T) %>% kable_styling( bootstrap_options = c("striped","hover", "bordered", "condensed"), latex_options = c("striped") ) %>% column_spec(1, color = "red") %>% add_header_above(c(" ", "Group A" = 2, "Group B" = 3)) Group A Group B mpg cyl disp hp drat Mazda RX4 21.0 6 160 110 3.90 Mazda RX4 Wag 21.0 6 160 110 3.90 Datsun 710 22.8 4 108 93 3.85 Hornet 4 Drive 21.4 6 258 110 3.08 Hornet Sportabout 18.7 8 360 175 3.15 One very important note here is that by default bookdown loads the gitbook table css on start up. It has some conflicts with bootstrap tables. As a result, some features like hover won’t be able to work by default. To solve this problem, you need to use the latest version of bookdown (version >= 0.7.21) and turn off the table_css option under bookdown::gitbook in _output.yml bookdown::gitbook: table_css: false 3.2 Epub Right now, it’s impossible to load addition CSS through HTML dependency and this mechanism exists for a reason ( See this issue I filed ). You will have to manually load this stylesheet by putting it to a CSS file (such as “style.css”) and load it in _output.yml. For example, bookdown::epub_book: stylesheet: style.css "] |