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

Conditional colouring with kableExtra() fails with NAs in column

我使用 kableExtra() 包在 PDF 输出 Rmarkdown 报告中对 tables 进行条件格式化。一切都很好,但如果在一列中存在 NA,则会失败。

原始报告包含在特定变量上具有百分比一致性的列(例如 21%(但 % 不在单元格中)),但是如果该值基于少于 10 个答案以保护受试者,它将显示 NA答案,因此我将在 table.

中有潜在的 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

编织时的错误是:

我还尝试添加第三个 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 

看来你有两个问题:

首先,我认为默认情况下没有设置颜色值。然而,对于某些单元格 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 = "")