Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thebioengineer/colortable

Add coloring and styling to data.frame and tibble records!
https://github.com/thebioengineer/colortable

Last synced: 3 days ago
JSON representation

Add coloring and styling to data.frame and tibble records!

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
warning = FALSE
)
```

# colortable

[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![R build status](https://github.com/thebioengineer/colortable/workflows/R-CMD-check/badge.svg)](https://github.com/thebioengineer/colortable/actions)
[![Codecov test coverage](https://codecov.io/gh/thebioengineer/colortable/branch/master/graph/badge.svg)](https://codecov.io/gh/thebioengineer/colortable?branch=master)

Seamlessly style and print your vectors across Rmarkdown output types through a single interface.
colortable enables users to style and color the contents of their vectors, data.frames, and tibbles through the object, and function, `color_vctr()`.

At this point, the supported output types include:


console
html
pdf
Microsoft Word
Beamer Presentations

## Installation

Currently {colortable} is only available on github, and is very much under development.

``` r
remotes::install_github("thebioengineer/colortable")
## install.packages("colortable") ## Not Available on CRAN
```

## Getting Started

{colortable} works by making a special S3 class called a `color_vctr`, and custom print/format functions.

It then has 4 arguments;
- The vector to be styled
- text_color, a vector that is either the color the entire vector to be colored or each element
- style, a vector is either the style the enture vector to be styled with or each element
- background, a vector that is either the background color the entire vector to be colored or for each element.

Additionally, there is a few helper functions
- `set_styling()` uses a boolean argument to apply the styling
- `color_scale()` is to be used for setting colors, accepting a pallette

*A note, html styling does not apply in a github readme*

```{r}
library(colortable)

color_vctr(c(1,2,3,4),
text_color = c("blue","green", "yellow",NA),
style = c("underline","italic",NA,"bold"),
background = c(NA,NA,"blue",NA))

color_vctr(LETTERS, text_color = color_scale(colorRamp(c("red","yellow"))))
color_vctr(LETTERS, text_color = color_scale("Blues"))

```

## Example

A common case I have seen for coloring values is from analysis coloring p-values.
Normally, when I have seen this the color is hard-coded in an ifelse statement with a paste0.
However, this liits the output to a single type.

The benefit of {{colortable}} is that the same code can be used across outputs and even in the console!

```{r example, eval = FALSE}
library(tidyverse)
library(knitr)

## Super Great analysis of mtcars!

lm_fit <- lm(mpg ~ ., mtcars)

a_lm_fit <- anova(lm_fit)

tbl_anova <- a_lm_fit %>%
as_tibble()%>%
mutate(
Coef = rownames(a_lm_fit),
`Pr(>F)` = set_styling(`Pr(>F)`, `Pr(>F)` < 0.05, background = "green", style = "underline"),
`Pr(>F)` = set_styling(`Pr(>F)`, is.na(`Pr(>F)`), style = "strikethrough", text_color = "silver"),
`F value` = set_styling(`F value`, is.na(`F value`), style = "strikethrough", text_color = "silver")
) %>%
select(Coef, everything())

kable(tbl_anova, escape = FALSE)

```

![examples](inst/media/output_gifs.gif)

## Output types

In order to simply generate a color_vctr, use the `color_vctr` function.
It can convert any atomic (numeric, integer, complex, character, logical, raw) into a color_vctr where text and background colors, and styles can be set.

To see the available styles and colors, use the `valid_*` family of functions: `valid_colors()`or `valid_style()`.
To check whether the styling is a valid type for the output, set the method to be "latex" for pdf outputs, or "html" for html outputs.

Below is a random sampling of output types to the console:

```{r, eval = FALSE}

data.frame(
text_color = sample(c(NA, valid_text_color()),10, replace = TRUE),
background = sample(c(NA, valid_background()),10, replace = TRUE),
style = sample(c(NA, valid_style()),10, replace = TRUE),
stringsAsFactors = FALSE
) %>%
mutate(
background = ifelse(text_color == background,
sample(c(NA, valid_background()),10, replace = TRUE),
background)
) %>%
mutate(
example = color_vctr(runif(10),
text_color = text_color,
background = background,
style = style)
)

```

![examples](inst/media/multiple_output_types.gif)

## Inspiration
This idea was inspired by [`crayon`](https://github.com/r-lib/crayon), and has some elements based on it. I thank all the developers of that project!
Since then, I have been insprired by ['flextable'](https://github.com/davidgohel/officedown) for word development.

Current styling technologies such as {kableExtra} and {formattable} also inspired the development of this project.

## COC

Please note that the 'colortable' project is released with a
[Contributor Code of Conduct](CODE_OF_CONDUCT.md).
By contributing to this project, you agree to abide by its terms.