-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
161 lines (113 loc) · 5.67 KB
/
README.Rmd
File metadata and controls
161 lines (113 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
warning = FALSE
)
```
# colortable <img src='man/figures/logo.png' align="right" height="200" />
<!-- badges: start -->
[](https://www.tidyverse.org/lifecycle/#experimental)
[](https://github.com/thebioengineer/colortable/actions)
[](https://codecov.io/gh/thebioengineer/colortable?branch=master)
<!-- badges: end -->
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:
<div style="display:inline-block">
<img alt="console" src="inst/media/console_logo.png" height = 80/>
<img alt="html" src="inst/media/html_logo.png" height = 80/>
<img alt="pdf" src="inst/media/pdf_logo.png" height = 80/>
<img alt="Microsoft Word" src="inst/media/microsoft_word_logo.png" height = 80/>
<img alt="Beamer Presentations" src="inst/media/beamer_pres.png" height = 80/>
</div>
## Installation
<!-- You can install the released version of colortable from [CRAN](https://CRAN.R-project.org) with: -->
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)
```

## 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)
)
```

## 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.