Hao Zhu | c145088 | 2018-10-03 17:56:26 -0400 | [diff] [blame] | 1 | # Cross-format Tables in Bookdown |
| 2 | |
| 3 | > 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. |
| 4 | |
| 5 | ## Use the "K-M" approach instead of "M-K" |
| 6 | Please read this chapter about the "K-M"/"M-K" approaches in `bookdown`: |
| 7 | https://bookdown.org/yihui/bookdown/new-session.html |
| 8 | |
| 9 | 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. |
| 10 | |
| 11 | 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. |
| 12 | |
| 13 | ``` |
| 14 | # Example _bookdown.yml |
| 15 | book_filename: "bookdown_example" |
| 16 | delete_merged_file: true |
| 17 | new_session: true |
| 18 | language: |
| 19 | ui: |
| 20 | chapter_name: "Chapter " |
| 21 | ``` |
| 22 | |
Hao Zhu | 5250ab5 | 2018-10-23 15:25:14 -0400 | [diff] [blame] | 23 | ## Prepare Your Tables for All Formats |
Hao Zhu | c145088 | 2018-10-03 17:56:26 -0400 | [diff] [blame] | 24 | 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`. |
| 25 | |
Hao Zhu | 5250ab5 | 2018-10-23 15:25:14 -0400 | [diff] [blame] | 26 | Here is an example for a table that will work in HTML, LaTeX & EPUB. |
Hao Zhu | c145088 | 2018-10-03 17:56:26 -0400 | [diff] [blame] | 27 | |
| 28 | ```{r} |
| 29 | library(kableExtra) |
Hao Zhu | 5250ab5 | 2018-10-23 15:25:14 -0400 | [diff] [blame] | 30 | library(dplyr) |
Hao Zhu | b809805 | 2018-10-08 16:57:43 -0400 | [diff] [blame] | 31 | options(kableExtra.html.bsTable = T) |
Hao Zhu | 5250ab5 | 2018-10-23 15:25:14 -0400 | [diff] [blame] | 32 | iris[1:10, ] %>% |
| 33 | mutate_if(is.numeric, function(x) { |
| 34 | cell_spec(x, bold = T, |
| 35 | color = spec_color(x, end = 0.9), |
| 36 | font_size = spec_font_size(x)) |
| 37 | }) %>% |
| 38 | mutate(Species = cell_spec( |
| 39 | Species, color = "white", bold = T, |
| 40 | background = spec_color(1:10, end = 0.9, |
| 41 | option = "A", direction = -1) |
| 42 | )) %>% |
| 43 | kable(escape = F, align = "c", booktabs = T) %>% |
| 44 | kable_styling(c("striped", "condensed"), |
| 45 | latex_options = "striped", |
| 46 | full_width = F) |
Hao Zhu | c145088 | 2018-10-03 17:56:26 -0400 | [diff] [blame] | 47 | ``` |