首页 > 解决方案 > kableExtra 表中具有多个案例的方程

问题描述

(编辑于 2019 年 7 月 11 日以包含问题group_rows()

我正在 Markdown 文档中制作一个表格,其中将包含不同情况的方程式。当我在 Markdown 中编写数组时,它会这样:

在此处输入图像描述

当我使用 将相同的方程式包含在表中kable()时,数组末尾的条件会被破坏:

编织成html后的表格

有谁知道如何让 kableExtra 表中的条件看起来像在表外一样?我希望最后的条件是一致的。手动添加空格 ( 0\\\ \\\ \\\ \\\ \\\ a = 0 \\\\) 看起来很糟糕。在继续使用更黑客的解决方案之前,我很想知道如何在 Markdown 中修复它。以下每种情况的代码。

Markdown 中的方程:

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

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()

```

提前感谢您的任何指导!

***** 编辑 7/11/19:上述问题已针对常规情况修复,但在group_rows()用于表格时发生:

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

结果是:

amps_everywhere

会话信息:

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

标签: rr-markdownxaringan

解决方案


更新:

正如@user2554330 建议的那样,从 github 安装最新的开发人员版本应该可以解决这个问题:

devtools::install_github("haozhu233/kableExtra")

老答案:

当您不使用kable_styling它时,它可以正常工作。不幸kable_styling的是,还没有escape争论。一种解决方法是手动替换转义符号:

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

在此处输入图像描述


推荐阅读