Skip to content

thebioengineer/colortable

Repository files navigation

colortable

Lifecycle: experimental R build status Codecov test coverage

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.

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

library(colortable)
#> Registered S3 methods overwritten by 'colortable':
#>   method                from     
#>   knit_print.data.frame rmarkdown
#>   print.data.frame      base

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))
## [1] 1 2 3 4 
color_vctr(LETTERS, text_color = color_scale(colorRamp(c("red","yellow"))))
## [1] A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
color_vctr(LETTERS, text_color = color_scale("Blues"))
## [1] A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 

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!

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

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:

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

Inspiration

This idea was inspired by crayon, and has some elements based on it. I thank all the developers of that project! Since then, I have been insprired by ‘flextable’ 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. By contributing to this project, you agree to abide by its terms.

About

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

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published