I'm trying to improve the readability of a rmarkdown document I'm working on. To that end, I'm trying to conditionally colour code my table output using kableExtra
so people can more easily skim read my report. I've managed to do some conditional formatting previously (e.g. if value > 0, colour it green), but I'm having trouble doing so when I have NA
in the variable by which I want to format my cells (if that makes sense).
I've included a reprex below which hopefully illustrates what I'm attempting to do. Sorry if it's a bit OTT!
library(tidyverse)
library(kableExtra)
# Example data
data <- tribble(
~direction, ~value,
"Ipsi", 25,
"Conv", 30,
"Contra", 9,
NA, 10
)
# Colours for directions
cols <- c(
"Ipsi" = "#CC6677",
"Conv" = "#2DA17E",
"Contra" = "#4477AA"
)
na.col = "#A5A5A5"
# Testing ifelse
data %>%
mutate(
simple_test = ifelse(
is.na(direction),
'direction is NA',
'direction is not NA'
)
)
#> # A tibble: 4 x 3
#> direction value simple_test
#> <chr> <dbl> <chr>
#> 1 Ipsi 25 direction is not NA
#> 2 Conv 30 direction is not NA
#> 3 Contra 9 direction is not NA
#> 4 <NA> 10 direction is NA
# Do colours show up as expected?
data %>%
mutate(
call_colours = ifelse(
is.na(direction),
na.col,
cols[direction]
)
)
#> # A tibble: 4 x 3
#> direction value call_colours
#> <chr> <dbl> <chr>
#> 1 Ipsi 25 #CC6677
#> 2 Conv 30 #2DA17E
#> 3 Contra 9 #4477AA
#> 4 <NA> 10 #A5A5A5
# Try combining with cell_spec from KableExtra; Results in error
data %>%
mutate(
call_cell_spec = ifelse(
is.na(direction),
cell_spec('direction is NA', background = na.col),
cell_spec(direction, background = cols[direction])
)
)
#> Error in mutate_impl(.data, dots): Evaluation error: missing value where TRUE/FALSE needed.
# Filtering out NA values in direction works
data %>%
filter(!is.na(direction)) %>%
mutate(
call_cell_spec = ifelse(
is.na(direction),
cell_spec('direction is NA', background = na.col),
cell_spec(direction, background = cols[direction])
)
)
#> # A tibble: 3 x 3
#> direction value call_cell_spec
#> <chr> <dbl> <chr>
#> 1 Ipsi 25 "<span style=\" border-radius: 4px; padding-right: ~
#> 2 Conv 30 "<span style=\" border-radius: 4px; padding-right: ~
#> 3 Contra 9 "<span style=\" border-radius: 4px; padding-right: ~
Created on 2018-10-24 by the reprex package (v0.2.1)