0

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

latex output

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) enter image description here

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

  • 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

Vincent
  • 10,769
  • 6
  • 31
  • 32
Cadbery
  • 11
  • 1

1 Answers1

0

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

enter image description here

Vincent
  • 10,769
  • 6
  • 31
  • 32
  • The kable_styling does get it to look better but this is still a HTML table. The first table I had in my question is the latex version of the table. My end goal is to be able to see the latex inline and save as png. I get only get the latex version when I knit to pdf. The table you have is still in HTML format so its not quite correcting for my current problem other than making my HTML version look better (which is appreciated still). At least this all what I am understanding at the moment. – Cadbery Sep 04 '20 at 03:16
  • Gotcha. That is what I understand to be the correct code to be as well. However when I do that "R Session Aborted" pops up and my R Studio terminates. Any idea what could cause that? It happens whenever I clarify "latex." I have tried tinytex and Miktext... – Cadbery Sep 04 '20 at 05:16
  • No, sorry. The code works on my mac (as attested by the screenshot in my post). I don't see a good reason for it to break on windows, but I don't have a machine to test it, and your error is unfortunately too vague for me to diagnose. – Vincent Sep 04 '20 at 10:25