Search code examples
rkablekableextra

How to use kableExtra as an `ifelse` argument with kable


I want to make a command of kableExtra as optional from kable. In this case, I want the footnote to be optional if the number of rows is greater than 1. See:

library(kableExtra)
ress2 <- structure(list(Constrate = c("RR - RX", "RR - XX", "RX - XX"), 
                        `Diferençe de Médias` = c(-0.96, -1.05, -0.09), 
                        `Erro Padrão` = c(0.63, 0.75, 0.63), 
                        `IC Inf (95%)` = c(-2.53, -2.92, -1.66), 
                        `IC Sup (95%)` = c(0.61, 0.82, 1.48), 
                        `Valor-p` = c(0.35, 0.43, 1)), 
                   row.names = c(NA,3L), class = "data.frame")
# It works
kable(ress2,
      align = c("l", rep("c", ncol(ress)-1))) %>%
  footnote(general = "d")

# What I have tried:
kable(ress2,
      align = c("l", rep("c", ncol(ress)-1))) %>% 
  { if(nrow(ress2)>1) add(., footnote(general = "d")) else .}

kable(ress2,
      align = c("l", rep("c", ncol(ress)-1))) %>% 
  { ifelse(nrow(ress2)>1,footnote(general = "d"),.) }

Solution

  • k <- kable(ress2, align = c("l", rep("c", ncol(ress)-1)))
    if(nrow(ress2)>1) {
      k <- k %>% footnote(general = "d")
    }
    k