使用kableExtra()进行条件着色时,列中的NA失败

时间:2019-02-04 16:03:23

标签: r r-markdown kableextra

我使用kableExtra()包对PDF输出的Rmarkdown报告中的表进行条件格式设置。一切正常,但如果在一个列中不存在NA,则失败。

原始报告的列中包含与特定变量(例如21%(但%不在单元格中))一致的百分比,但是如果该值基于少于10个答案来保护主题答案,则会显示NA。因此表中将有潜在的NA。

在这种情况下,它可以正常工作(不适用):

---
output: pdf_document
---

```{r setup}
knitr::opts_chunk$set(echo = TRUE)
# load packages
library(tidyverse)
library(kableExtra)


# example when no NA are resent
df1 <- mtcars %>% select(mpg, cyl, disp)
df1 <- head(df1, 10)
# kable
df1 %>%
  mutate(cyl = cell_spec(cyl, color = if_else(cyl == 8, "teal",
                                              if_else(cyl == 6, "orange", 
                                                      if_else(is.na(cyl), "black", "red"))))) %>%
    kable("latex", escape = F, booktabs = TRUE, linesep = "")

```

当存在NA时,这是另一个:

```{r, eval=T}
# example when NA is present
df2 <- mtcars %>% select(mpg, cyl, disp)
df2 <- head(df2, 10)
df2$cyl[df2$cyl == 4] <- NA

# kable
df2 %>%
  mutate(cyl = cell_spec(cyl, color = if_else(cyl == 8, "teal",
                                              if_else(cyl == 6, "orange", "red")))) %>%
    kable("latex", escape = F, booktabs = TRUE, linesep = "")


```

运行块的错误是:

Error in if (substr(color, 1, 1) != "#") { : missing value where 
TRUE/FALSE needed

编织时的错误是:

enter image description here

我还尝试添加第三个if_else(),其值为is.na()-错误保持不变:

# kable
df2 %>%
  mutate(cyl = cell_spec(cyl, color = if_else(cyl == 8, "teal",
                                              if_else(cyl == 6, "orange", 
                                                      if_else(is.na(cyl), "black","red"))))) %>%
    kable("latex", escape = F, booktabs = TRUE, linesep = "")

我的session.info:

R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.2

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] naniar_0.4.1       shurp2018_0.0.9000 kableExtra_1.0.1   forcats_0.3.0      stringr_1.3.1      dplyr_0.8.0        purrr_0.3.0       
 [8] readr_1.3.1        tidyr_0.8.2        tibble_2.0.99.9000 ggplot2_3.1.0      tidyverse_1.2.1 

1 个答案:

答案 0 :(得分:1)

看来您有两个问题:

首先,我认为默认情况下未设置颜色值。但是,对于某些单元格color = NA,它也不会被覆盖。因此,您可以通过设置color = NULL来解决此问题(其他任何颜色都将与下一步分配的颜色并置,这会造成麻烦)。因此这对我来说有些有用:

```{r}
# kable
df2 %>%
  mutate(cyl = cell_spec(cyl, color = NULL)) %>% 
  mutate(cyl = cell_spec(cyl, color = if_else(cyl == 8, "teal",
                                              if_else(cyl == 6, "orange", "red")))) %>%
    kable("latex", escape = F, booktabs = TRUE, linesep = "")
```

问题是NA的颜色现在是红色,而不是黑色。

第二个问题是您的ifelse评估无法正常工作

您还可以分别检查ifelse函数中发生的情况,以更好地了解此问题:

> with(df2,  if_else(cyl == 8, "teal",
+                    if_else(cyl == 6, "orange", 
+                            if_else(is.na(cyl), "black","red"))))
 [1] "orange" "orange" NA       "orange" "teal"   "orange" "teal"   NA       NA       "orange"

但是您可以通过首先评估is.na(cyl)而不是最后一次来使它起作用:

df2 %>%
  mutate(cyl = cell_spec(cyl, color = if_else(is.na(cyl), "black", 
                                              if_else(cyl == 8, "teal",
                                                      if_else(cyl == 6, "orange", "red"))))) %>%
  kable("latex", escape = F, booktabs = TRUE, linesep = "")