kableExtra:无法使用数学表达式保存 HTML table 并且无法使用 Latex table 显示内联历史记录

kableExtra: Cannot save HTML table with math expressions & can't get in-line hist to display with Latex table

我想在 R 中将 table 保存为图像,其中既包含列名称中的 数学表达式 又包含 内联直方图 来自 kableExtra 中的 spec_hist() 函数。理想情况下,我希望 table 格式为 Latex table,但 HTML 也可以。

我遇到的问题是,当我尝试 保存 一个 kable HTML table 时,数学表达式没有被正确解释,但是在-line 直方图显示得很好。

同时,如果我尝试保存 kable Latex table,数学表达式会被正确解释,但直方图不会出现。

这可能是 kableExtra 中的一个错误(两个错误?)?还是我遗漏了什么?

请注意,kable HTML table 在 R Studio/R Markdown 中看起来非常好(数学表达式和内联图)。但我想保存一个重复使用的图像。 kable Latex table 然而从不显示内联直方图,无论是在 R 中显示还是保存到文件中。

这是一个可重现的例子:

library(knitr)
library(kableExtra)

df <- mtcars[1:6,] %>% 
          mutate(`$\theta_{boot}$` = "") %>% # add an empty column where the in-line histograms will go
          rename(`$mpg$` = mpg, # rename some columns to math expressions
                 `$\bar{cyl}$` = cyl, 
                 `$\bar{disp}$` = disp,
                 `$hp$` = hp, 
                 `$drat$` = drat)

# create dummy data for the in-line histograms
hist_dat <- list(runif(20), runif(20), runif(20), runif(20), runif(20), runif(20))


# Make HTML Table
kbl(df, escape = FALSE, booktabs = T, format = "html") %>%
  add_header_above(c("Some Header" = 6, "Some Other Header" = 7)) %>% # add header
  kable_styling(latex_options = c("repeat_header"), font_size = 12) %>%
  column_spec(12, image = spec_hist(hist_dat)) %>% # add in-line hist
  pack_rows("Group 1", 1, 3) %>%
  pack_rows("Group 2", 4, 6) %>%
  kable_paper() %>%
  as_image(file = 'temp.png')
  #save_kable(file = 'temp.png')

上面保存这张图片:

有直方图,但列名打印为文字而不是解释的数学表达式。

现在是 Latex 版本:

# Make Latex Table
kbl(df, escape = FALSE, booktabs = T, format = "latex") %>%
  add_header_above(c("Some Header" = 6, "Some Other Header" = 7)) %>% 
  kable_styling(latex_options = c("repeat_header"), font_size = 12) %>%
  column_spec(12, image = spec_hist(hist_dat)) %>%  
  pack_rows("Group 1", 1, 3) %>%
  pack_rows("Group 2", 4, 6) %>%
  kable_paper(full_width = TRUE) %>%
  as_image(file = 'temp_latex.png')
  #save_kable(file = 'temp.png')

使用 format = "latex" 和 full_width = TRUE 生成以下文件:

现在可以正确解释列名,但不会绘制直方图。

我也发现用as_image()save_kable()保存图片没有区别

有什么想法吗?

谢谢!

编辑: 硬件说明:我在 Mac 上安装了 LaTeX。图形工作正常,尽管所有涉及乳胶的调用在我的 Windows 机器上都失败了(相同的代码,安装了 MikTeX、Ghostscript 和 Magick)。

> R.version
               _                           
platform       x86_64-apple-darwin17.0     
arch           x86_64                      
os             darwin17.0                  
system         x86_64, darwin17.0          
status                                     
major          4                           
minor          1.0                         
year           2021                        
month          05                          
day            18                          
svn rev        80317                       
language       R                           
version.string R version 4.1.0 (2021-05-18)
nickname       Camp Pontanezen 

如果你编织了rmarkdown文档,那么会生成pdf文档(文档中包含图片),并且图片会单独保存为文件。

似乎需要调整列规范以考虑行名称。

添加列宽有助于管理页面上的 table。

```{r, results='asis'}

library(knitr)
library(kableExtra)
library(dplyr)


kable(df,
      format = "latex",
      escape = FALSE,
      booktabs = TRUE) %>%
  add_header_above(c("Some Header" = 6, "Some Other Header" = 7)) %>%
  kable_styling(font_size = 8, full_width = TRUE) %>%
  column_spec(1, width = "30mm") %>% 
  column_spec(2:12, width = "6mm") %>%
  column_spec(13, image = spec_hist(hist_dat, col = "red")) %>%  
  pack_rows("Group 1", 1, 3) %>%
  pack_rows("Group 2", 4, 6) %>%
  as_image(file = 'temp/as-image.png') 
  # as_image(file = 'temp/as-image.pdf') # works as pdf as well
```