kableExtra HTML styling in Rmarkdown and kable_save()

99封情书 提交于 2021-01-29 21:49:10

问题


This has plagued me for too long, I would appreciate help. I have been investing time into kable, but it hasn't quite worked out for me the way I would like. I am looking to create multi grouped rows as in the image

The code to do this is as follows:

collapse_rows_dt <- expand.grid( District = sprintf('District %s', c('1', '2')), City = sprintf('City %s', c('1', '2')), State = sprintf('State %s', c('a', 'b')), Country = sprintf('Country with a long name %s', c('A', 'B'))
)
collapse_rows_dt <- collapse_rows_dt[c("Country", "State", "City", "District")] 
collapse_rows_dt$C1 = rnorm(nrow(collapse_rows_dt)) 
collapse_rows_dt$C2 = rnorm(nrow(collapse_rows_dt))

kbl(collapse_rows_dt, booktabs = T, align = "c", linesep = '') %>%
collapse_rows(1:3, row_group_label_position = 'stack')

The problem is that when I run this in R markdown I get the HTML version in the output (see below)

This obviously is not good enough. I CAN get the correct output (first image) if I knit to pdf, but that's it. If I try doing save_kable() it turns out in the HTML format as in the second image. Kniting to pdf every time is so impractical that I can't possible use kable anymore if I can't fix this. That is a big deal for me.

If I set format='latex' then nothing shows up inline and when I try to kable_save() I get this error message:

this is Xtex version 3....(tex live 2020/w32Tex) preloaded format=xlatex) restricted \write18 enabled. Entering extended mode

Followed by a pop that informs me (R crashes)

R session aborted, r encoutered a fatal error

All HTML tables that don't require latex show up inline appropriately and will save as their actual image.

Relevant Up to Date Packages:

-     library(webshot) 
      library(tinytex) (also tried without)
      library(magick) 
      library(plyr) 
      library(tidyverse) 
      library(dplyr)
      library(knitr) 
      library(skimr) 
      library(kableExtra)

Also:

  • Ghostscript 9.52 is current and set to environment
  • Miktext 2.9

Have Tried:

  • updating imageMagick via install.packages(magick

    • editing the policy.xls in ImageMagick to bypass a security feature ImageMagick security policy 'PDF' blocking conversion
  • Manually installing the following Latex packages

    • library(tinytex)tlmgr_install(pkgs = 'standalone')tlmgr_install(pkgs = 'preview')tlmgr_install(pkgs = 'polyglossia')tlmgr_install(pkgs = 'xltxtra')tlmgr_install(pkgs = 'realscripts')
  • Setting Mixtex to environment path


回答1:


If you want to knit and include the image produced via LaTeX and PDF, you can use kableExtra::as_image

---
output: html_document
---

```{r}
library(kableExtra)
library(magrittr)

collapse_rows_dt <- expand.grid( District = sprintf('District %s', c('1', '2')), City = sprintf('City %s', c('1', '2')), State = sprintf('State %s', c('a', 'b')), Country = sprintf('Country with a long name %s', c('A', 'B'))
)

collapse_rows_dt <- collapse_rows_dt[c("Country", "State", "City", "District")] 
collapse_rows_dt$C1 = rnorm(nrow(collapse_rows_dt)) 
collapse_rows_dt$C2 = rnorm(nrow(collapse_rows_dt))

kbl(collapse_rows_dt, "latex", align="c", linesep="", booktabs = T) %>%
  collapse_rows(1:3, row_group_label_position = 'stack') %>%
  kable_styling(latex_options = c("striped", "scale_down")) %>%
  as_image()
```



来源:https://stackoverflow.com/questions/63733482/kableextra-html-styling-in-rmarkdown-and-kable-save

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!