remove dplyr from dependencies
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.