Search code examples
rr-markdownxaringan

Equations with multiple cases in a kableExtra table


(edited 7/11/2019 to include issue with group_rows() )

I am making a table in a Markdown document that will include equations with different cases. When I write the array in Markdown, it knits to this:

enter image description here

When I include the same equation in a table using kable(), the conditions at the end of the array are mangled:

table after knitting to html

Does anyone know how to get conditions in kableExtra tables to look like they do outside the tables? I want the conditions at the end to be aligned. Manually adding spaces (0\\\ \\\ \\\ \\\ \\\ a = 0 \\\\) looks bad. I would love to know how to fix it in Markdown before proceeding to hackier solutions. Code for each case below.

Equation in Markdown:

 $$C_{y,a}=
    \begin{cases}
    0 &   a=0 \\ 
    \frac{C_y N_{y,a}}{N_y^{1+}} & a>0 \\
    \end{cases}$$

Same equation in a table in Markdown:

 **Table 1.** Population dynamics.
```{r echo = F}
Equation_number <- c(1,2)

Equation <- c("$N_{i1,y} = R_{i,y} = R_{0,i }e^{\\tau_{i,y}}$",
              "$C_{y,a}=
              \\begin{cases}
              0 &   a=0 \\\\
              \\frac{C_y N_{y,a}}{N_y^{1+}} & a>0 \\\\
              \\end{cases}$")

Description <- c("Initial numbers at age","Catches at age")

Population_Equations <- data.frame(cbind(Equation_number,
                                         Equation,
                                         Description))
colnames(Population_Equations) = c("Eq.",
                                   "Equation", 
                                   "Description")
knitr::kable(format="html",
             Population_Equations, 
             escape = FALSE) %>% 
            kable_styling()

```

Thank you in advance for any guidance!

***** EDIT 7/11/19: The above issue has been fixed for regular cases but occurs when group_rows() is used for the table:

knitr::kable(format="html",
             Population_Equations, 
             escape = FALSE) %>%
             group_rows(index=c("First group"=1, "Second group"=1)) %>%
             kable_styling()

Results in:

amps_everywhere

Session info:

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

Random number generation:
 RNG:     Mersenne-Twister 
 Normal:  Inversion 
 Sample:  Rounding 

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

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

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.1        rstudioapi_0.10   knitr_1.23        magrittr_1.5      usethis_1.5.1     devtools_2.1.0    pkgload_1.0.2    
 [8] R6_2.4.0          rlang_0.4.0       tools_3.6.1       pkgbuild_1.0.3    xfun_0.8          sessioninfo_1.1.1 cli_1.1.0        
[15] withr_2.1.2       remotes_2.1.0     htmltools_0.3.6   yaml_2.2.0        assertthat_0.2.1  digest_0.6.20     rprojroot_1.3-2  
[22] crayon_1.3.4      processx_3.4.0    callr_3.3.0       fs_1.3.1          ps_1.3.0          curl_3.3          rsconnect_0.8.13 
[29] testthat_2.1.1    glue_1.3.1        memoise_1.1.0     evaluate_0.14     rmarkdown_1.13    compiler_3.6.1    desc_1.2.0       
[36] backports_1.1.4   prettyunits_1.0.2

Solution

  • Update:

    As @user2554330 suggests, installing the newest developer version from github should fix this:

    devtools::install_github("haozhu233/kableExtra")
    

    Old answer:

    When you do not use kable_styling it works fine. Unfortunetly, kable_styling has not the escape argument yet. A workaround would be to manually replace the escaped symbols:

    myTable <- knitr::kable(format="html",
                            Population_Equations, 
                            escape = FALSE) %>% 
      kable_styling()
    myTable <- gsub("&amp;", "&", myTable) 
    myTable <- gsub("&gt;", ">", myTable) 
    

    enter image description here