remove dplyr from dependencies
diff --git a/docs/awesome_table_in_html.Rmd b/docs/awesome_table_in_html.Rmd
index 9b1eea9..9cca865 100644
--- a/docs/awesome_table_in_html.Rmd
+++ b/docs/awesome_table_in_html.Rmd
@@ -243,42 +243,66 @@
Currently, `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.
## Conditional logic
+>**Key Update: Again, as said before, if you are using kableExtra 1.2+, you are now recommended to used `column_spec` to do conditional formatting**.
+
It is very easy to use `cell_spec` with conditional logic. Here is an example.
```{r, message=FALSE, warning=FALSE}
-library(dplyr)
-mtcars[1:10, 1:2] %>%
- mutate(
- car = row.names(.),
- mpg = cell_spec(mpg, "html", color = ifelse(mpg > 20, "red", "blue")),
- cyl = cell_spec(cyl, "html", color = "white", align = "c", angle = 45,
- background = factor(cyl, c(4, 6, 8),
- c("#666666", "#999999", "#BBBBBB")))
- ) %>%
- select(car, mpg, cyl) %>%
- kbl(format = "html", escape = F) %>%
- kable_styling("striped", full_width = F)
+cs_dt <- mtcars[1:10, 1:2]
+cs_dt$car = row.names(cs_dt)
+row.names(cs_dt) <- NULL
+cs_dt$mpg = cell_spec(cs_dt$mpg, color = ifelse(cs_dt$mpg > 20, "red", "blue"))
+cs_dt$cyl = cell_spec(
+ cs_dt$cyl, color = "white", align = "c", angle = 45,
+ background = factor(cs_dt$cyl, c(4, 6, 8), c("#666666", "#999999", "#BBBBBB")))
+cs_dt <- cs_dt[c("car", "mpg", "cyl")]
+
+kbl(cs_dt, escape = F) %>%
+ kable_paper("striped", full_width = F)
+
+# You can also do this with dplyr and use one pipe from top to bottom
+# library(dplyr)
+# mtcars[1:10, 1:2] %>%
+# mutate(
+# car = row.names(.),
+# mpg = cell_spec(mpg, "html", color = ifelse(mpg > 20, "red", "blue")),
+# cyl = cell_spec(cyl, "html", color = "white", align = "c", angle = 45,
+# background = factor(cyl, c(4, 6, 8),
+# c("#666666", "#999999", "#BBBBBB")))
+# ) %>%
+# select(car, mpg, cyl) %>%
+# kbl(format = "html", escape = F) %>%
+# kable_styling("striped", full_width = F)
```
## Visualize data with Viridis Color
This package also comes with a few helper functions, including `spec_color`, `spec_font_size` & `spec_angle`. These functions can rescale continuous variables to certain scales. For example, function `spec_color` would map a continuous variable to any [viridis color palettes](https://CRAN.R-project.org/package=viridisLite). It offers a very visually impressive representation in a tabular format.
```{r}
-iris[1:10, ] %>%
- mutate_if(is.numeric, function(x) {
+vs_dt <- iris[1:10, ]
+vs_dt[1:4] <- lapply(vs_dt[1:4], 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)
- )) %>%
- kbl(escape = F, align = "c") %>%
- kable_styling(c("striped", "condensed"), full_width = F)
+})
+vs_dt[5] <- cell_spec(vs_dt[[5]], color = "white", bold = T,
+ background = spec_color(1:10, end = 0.9, option = "A", direction = -1))
+kbl(vs_dt, escape = F, align = "c") %>%
+ kable_classic("striped", full_width = F)
+# Or dplyr ver
+# 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") %>%
+# kable_styling(c("striped", "condensed"), full_width = F)
```
-In the example above, I'm using the `mutate` functions from `dplyr`. You don't have to use it. Base R solutions like `iris$Species <- cell_spec(iris$Species, color = "red")` also works.
-
## Text Specification
If you check the results of `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 `rmarkdown` document or write some text in shiny apps, if you need extra markups other than **bold** or *italic*, you may use this function to `r text_spec("color", color = "red")`, `r text_spec("change font size ", font_size = 16)` or `r text_spec("rotate", angle = 30)` your text.
@@ -351,19 +375,21 @@
You can combine the good parts from `kableExtra` & `formattable` together into one piece. Read more at http://haozhu233.github.io/kableExtra/use_kableExtra_with_formattable.html
```{r, message = FALSE, warning=FALSE}
library(formattable)
-mtcars[1:5, 1:4] %>%
- mutate(
- car = row.names(.),
- mpg = color_tile("white", "orange")(mpg),
- cyl = cell_spec(cyl, angle = (1:5)*60,
- background = "red", color = "white", align = "center"),
- disp = ifelse(disp > 200,
- cell_spec(disp, color = "red", bold = T),
- cell_spec(disp, color = "green", italic = T)),
- hp = color_bar("lightgreen")(hp)
- ) %>%
- select(car, everything()) %>%
- kbl(escape = F) %>%
+ft_dt <- mtcars[1:5, 1:4]
+ft_dt$car <- row.names(ft_dt)
+row.names(ft_dt) <- NULL
+ft_dt$mpg <- color_tile("white", "orange")(ft_dt$mpg)
+ft_dt$cyl <- cell_spec(ft_dt$cyl, angle = (1:5)*60,
+ background = "red", color = "white", align = "center")
+ft_dt$disp <- ifelse(
+ ft_dt$disp > 200,
+ cell_spec(ft_dt$disp, color = "red", bold = T),
+ cell_spec(ft_dt$disp, color = "green", italic = T)
+)
+ft_dt$hp <- color_bar("lightgreen")(ft_dt$hp)
+ft_dt <- ft_dt[c("car", "mpg", "cyl", "disp", "hp")]
+
+kbl(ft_dt, escape = F) %>%
kable_styling("hover", full_width = F) %>%
column_spec(5, width = "3cm") %>%
add_header_above(c(" ", "Hello" = 2, "World" = 2))
diff --git a/docs/awesome_table_in_html.html b/docs/awesome_table_in_html.html
index 40f4a07..2aa9ce2 100644
--- a/docs/awesome_table_in_html.html
+++ b/docs/awesome_table_in_html.html
@@ -4857,20 +4857,22 @@
<p>Currently, <code>cell_spec</code> 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.</p>
<div id="conditional-logic" class="section level2">
<h2>Conditional logic</h2>
+<blockquote>
+<p><strong>Key Update: Again, as said before, if you are using kableExtra 1.2+, you are now recommended to used <code>column_spec</code> to do conditional formatting</strong>.</p>
+</blockquote>
<p>It is very easy to use <code>cell_spec</code> with conditional logic. Here is an example.</p>
-<pre class="r"><code>library(dplyr)
-mtcars[1:10, 1:2] %>%
- mutate(
- car = row.names(.),
- mpg = cell_spec(mpg, "html", color = ifelse(mpg > 20, "red", "blue")),
- cyl = cell_spec(cyl, "html", color = "white", align = "c", angle = 45,
- background = factor(cyl, c(4, 6, 8),
- c("#666666", "#999999", "#BBBBBB")))
- ) %>%
- select(car, mpg, cyl) %>%
- kbl(format = "html", escape = F) %>%
- kable_styling("striped", full_width = F)</code></pre>
-<table class="table table-striped" style="width: auto !important; margin-left: auto; margin-right: auto;">
+<pre class="r"><code>cs_dt <- mtcars[1:10, 1:2]
+cs_dt$car = row.names(cs_dt)
+row.names(cs_dt) <- NULL
+cs_dt$mpg = cell_spec(cs_dt$mpg, color = ifelse(cs_dt$mpg > 20, "red", "blue"))
+cs_dt$cyl = cell_spec(
+ cs_dt$cyl, color = "white", align = "c", angle = 45,
+ background = factor(cs_dt$cyl, c(4, 6, 8), c("#666666", "#999999", "#BBBBBB")))
+cs_dt <- cs_dt[c("car", "mpg", "cyl")]
+
+kbl(cs_dt, escape = F) %>%
+ kable_paper("striped", full_width = F)</code></pre>
+<table class=" lightable-paper lightable-striped" style="font-family: "Arial Narrow", arial, helvetica, sans-serif; width: auto !important; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;">
@@ -4997,23 +4999,34 @@
</tr>
</tbody>
</table>
+<pre class="r"><code># You can also do this with dplyr and use one pipe from top to bottom
+# library(dplyr)
+# mtcars[1:10, 1:2] %>%
+# mutate(
+# car = row.names(.),
+# mpg = cell_spec(mpg, "html", color = ifelse(mpg > 20, "red", "blue")),
+# cyl = cell_spec(cyl, "html", color = "white", align = "c", angle = 45,
+# background = factor(cyl, c(4, 6, 8),
+# c("#666666", "#999999", "#BBBBBB")))
+# ) %>%
+# select(car, mpg, cyl) %>%
+# kbl(format = "html", escape = F) %>%
+# kable_styling("striped", full_width = F)</code></pre>
</div>
<div id="visualize-data-with-viridis-color" class="section level2">
<h2>Visualize data with Viridis Color</h2>
<p>This package also comes with a few helper functions, including <code>spec_color</code>, <code>spec_font_size</code> & <code>spec_angle</code>. These functions can rescale continuous variables to certain scales. For example, function <code>spec_color</code> would map a continuous variable to any <a href="https://CRAN.R-project.org/package=viridisLite">viridis color palettes</a>. It offers a very visually impressive representation in a tabular format.</p>
-<pre class="r"><code>iris[1:10, ] %>%
- mutate_if(is.numeric, function(x) {
+<pre class="r"><code>vs_dt <- iris[1:10, ]
+vs_dt[1:4] <- lapply(vs_dt[1:4], 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)
- )) %>%
- kbl(escape = F, align = "c") %>%
- kable_styling(c("striped", "condensed"), full_width = F)</code></pre>
-<table class="table table-striped table-condensed" style="width: auto !important; margin-left: auto; margin-right: auto;">
+})
+vs_dt[5] <- cell_spec(vs_dt[[5]], color = "white", bold = T,
+ background = spec_color(1:10, end = 0.9, option = "A", direction = -1))
+kbl(vs_dt, escape = F, align = "c") %>%
+ kable_classic("striped", full_width = F)</code></pre>
+<table class=" lightable-classic lightable-striped" style="font-family: "Arial Narrow", "Source Sans Pro", sans-serif; width: auto !important; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:center;">
@@ -5206,7 +5219,6 @@
</tr>
</tbody>
</table>
-<p>In the example above, I’m using the <code>mutate</code> functions from <code>dplyr</code>. You don’t have to use it. Base R solutions like <code>iris$Species <- cell_spec(iris$Species, color = "red")</code> also works.</p>
</div>
<div id="text-specification" class="section level2">
<h2>Text Specification</h2>
@@ -5312,19 +5324,21 @@
<h2>Integration with <code>formattable</code></h2>
<p>You can combine the good parts from <code>kableExtra</code> & <code>formattable</code> together into one piece. Read more at <a href="http://haozhu233.github.io/kableExtra/use_kableExtra_with_formattable.html" class="uri">http://haozhu233.github.io/kableExtra/use_kableExtra_with_formattable.html</a></p>
<pre class="r"><code>library(formattable)
-mtcars[1:5, 1:4] %>%
- mutate(
- car = row.names(.),
- mpg = color_tile("white", "orange")(mpg),
- cyl = cell_spec(cyl, angle = (1:5)*60,
- background = "red", color = "white", align = "center"),
- disp = ifelse(disp > 200,
- cell_spec(disp, color = "red", bold = T),
- cell_spec(disp, color = "green", italic = T)),
- hp = color_bar("lightgreen")(hp)
- ) %>%
- select(car, everything()) %>%
- kbl(escape = F) %>%
+ft_dt <- mtcars[1:5, 1:4]
+ft_dt$car <- row.names(ft_dt)
+row.names(ft_dt) <- NULL
+ft_dt$mpg <- color_tile("white", "orange")(ft_dt$mpg)
+ft_dt$cyl <- cell_spec(ft_dt$cyl, angle = (1:5)*60,
+ background = "red", color = "white", align = "center")
+ft_dt$disp <- ifelse(
+ ft_dt$disp > 200,
+ cell_spec(ft_dt$disp, color = "red", bold = T),
+ cell_spec(ft_dt$disp, color = "green", italic = T)
+)
+ft_dt$hp <- color_bar("lightgreen")(ft_dt$hp)
+ft_dt <- ft_dt[c("car", "mpg", "cyl", "disp", "hp")]
+
+kbl(ft_dt, escape = F) %>%
kable_styling("hover", full_width = F) %>%
column_spec(5, width = "3cm") %>%
add_header_above(c(" ", "Hello" = 2, "World" = 2))</code></pre>
@@ -6452,7 +6466,7 @@
1
</td>
<td style="text-align:center;">
-0
+1
</td>
</tr>
<tr>
@@ -6484,7 +6498,7 @@
5
</td>
<td style="text-align:center;">
-0
+1
</td>
</tr>
<tr>
@@ -6492,7 +6506,7 @@
6
</td>
<td style="text-align:center;">
-0
+1
</td>
</tr>
<tr>
@@ -6500,7 +6514,7 @@
7
</td>
<td style="text-align:center;">
-1
+0
</td>
</tr>
<tr>
@@ -6511,7 +6525,7 @@
8
</td>
<td style="text-align:center;">
-0
+1
</td>
</tr>
<tr>
@@ -6527,7 +6541,7 @@
10
</td>
<td style="text-align:center;">
-1
+0
</td>
</tr>
<tr>
@@ -6541,7 +6555,7 @@
11
</td>
<td style="text-align:center;">
-1
+0
</td>
</tr>
<tr>
@@ -6568,7 +6582,7 @@
14
</td>
<td style="text-align:center;">
-1
+0
</td>
</tr>
<tr>
@@ -6576,7 +6590,7 @@
15
</td>
<td style="text-align:center;">
-1
+0
</td>
</tr>
</tbody>
diff --git a/docs/awesome_table_in_pdf.Rmd b/docs/awesome_table_in_pdf.Rmd
index fc1147e..db06969 100644
--- a/docs/awesome_table_in_pdf.Rmd
+++ b/docs/awesome_table_in_pdf.Rmd
@@ -273,38 +273,62 @@
## Conditional logic
It is very easy to use `cell_spec` with conditional logic. Here is an example.
```{r, message=FALSE, warning=FALSE}
-library(dplyr)
-mtcars[1:10, 1:2] %>%
- mutate(
- car = row.names(.),
- # You don't need format = "latex" if you have ever defined options(knitr.table.format)
- mpg = cell_spec(mpg, color = ifelse(mpg > 20, "red", "blue")),
- cyl = cell_spec(cyl, color = "white", align = "c", angle = 45,
- background = factor(cyl, c(4, 6, 8),
- c("#666666", "#999999", "#BBBBBB")))
- ) %>%
- select(car, mpg, cyl) %>%
- kbl(escape = F, booktabs = T, linesep = "")
+cs_dt <- mtcars[1:10, 1:2]
+cs_dt$car = row.names(cs_dt)
+row.names(cs_dt) <- NULL
+cs_dt$mpg = cell_spec(cs_dt$mpg, color = ifelse(cs_dt$mpg > 20, "red", "blue"))
+cs_dt$cyl = cell_spec(
+ cs_dt$cyl, color = "white", align = "c", angle = 45,
+ background = factor(cs_dt$cyl, c(4, 6, 8), c("#666666", "#999999", "#BBBBBB")))
+cs_dt <- cs_dt[c("car", "mpg", "cyl")]
+
+kbl(cs_dt, escape = F) %>%
+ kable_paper("striped", full_width = F)
+
+# You can also do this with dplyr and use one pipe from top to bottom
+# library(dplyr)
+# mtcars[1:10, 1:2] %>%
+# mutate(
+# car = row.names(.),
+# mpg = cell_spec(mpg, "html", color = ifelse(mpg > 20, "red", "blue")),
+# cyl = cell_spec(cyl, "html", color = "white", align = "c", angle = 45,
+# background = factor(cyl, c(4, 6, 8),
+# c("#666666", "#999999", "#BBBBBB")))
+# ) %>%
+# select(car, mpg, cyl) %>%
+# kbl(format = "html", escape = F) %>%
+# kable_styling("striped", full_width = F)
```
## Visualize data with Viridis Color
This package also comes with a few helper functions, including `spec_color`, `spec_font_size` & `spec_angle`. These functions can rescale continuous variables to certain scales. For example, function `spec_color` would map a continuous variable to any [viridis color palettes](https://CRAN.R-project.org/package=viridisLite). It offers a very visually impactful representation in a tabular format.
```{r}
-iris[1:10, ] %>%
- mutate_if(is.numeric, function(x) {
- cell_spec(x, bold = T, color = spec_color(x, end = 0.9),
+vs_dt <- iris[1:10, ]
+vs_dt[1:4] <- lapply(vs_dt[1:4], 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)
- )) %>%
- kbl(escape = F, booktabs = T, linesep = "", align = "c")
+})
+vs_dt[5] <- cell_spec(vs_dt[[5]], color = "white", bold = T,
+ background = spec_color(1:10, end = 0.9, option = "A", direction = -1))
+kbl(vs_dt, escape = F, align = "c") %>%
+ kable_classic("striped", full_width = F)
+# Or dplyr ver
+# 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") %>%
+# kable_styling(c("striped", "condensed"), full_width = F)
```
-In the example above, I'm using the `mutate` functions from `dplyr`. You don't have to use it. Base R solutions like `iris$Species <- cell_spec(iris$Species, color = "red")` also works.
-
## Text Specification
If you check the results of `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 `rmarkdown` document or write some text in shiny apps, if you need extra markups other than **bold** or *italic*, you may use this function to `r text_spec("color", color = "red")`, `r text_spec("change font size ", font_size = 16)` or `r text_spec("rotate", angle = 30)` your text.
@@ -424,14 +448,14 @@
```{r}
collapse_rows_dt <- expand.grid(
- Country = sprintf('Country with a long name %s', c('A', 'B')),
- State = sprintf('State %s', c('a', 'b')),
+ District = sprintf('District %s', c('1', '2')),
City = sprintf('City %s', c('1', '2')),
- District = sprintf('District %s', c('1', '2'))
-) %>% arrange(Country, State, City) %>%
- mutate_all(as.character) %>%
- mutate(C1 = rnorm(n()),
- C2 = rnorm(n()))
+ State = sprintf('State %s', c('a', 'b')),
+ Country = sprintf('Country with a long name %s', c('A', 'B'))
+)
+collapse_rows_dt <- collapse_rows_dt[c("Country", "State", "City", "District")]
+collapse_rows_dt$C1 = rnorm(nrow(collapse_rows_dt))
+collapse_rows_dt$C2 = rnorm(nrow(collapse_rows_dt))
kbl(collapse_rows_dt,
booktabs = T, align = "c", linesep = '') %>%
@@ -519,11 +543,15 @@
Item = c("Hello\nWorld", "This\nis a cat"),
Value = c(10, 100)
)
+dt_lb$Item = linebreak(Item)
+
+# Or you can use
+# dt_lb <- dt_lb %>%
+# mutate_all(linebreak)
dt_lb %>%
- mutate_all(linebreak) %>%
kbl(booktabs = T, escape = F,
- col.names = linebreak(c("Item\n(Name)", "Value\n(Number)"), align = "c"))
+ col.names = linebreak(c("Item\n(Name)", "Value\n(Number)"), align = "c"))
```
At the same time, since `kableExtra 0.8.0`, all `kableExtra` functions that have some contents input (such as `footnote` or `pack_rows`) will automatically convert `\n` to linebreaks for you in both LaTeX and HTML.