haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
688 stars 147 forks source link

group_rows not working properly for latex tables #366

Open JamesCuster opened 5 years ago

JamesCuster commented 5 years ago

I am having almost the exact same issue as reported in #254, but the fix that was applied does not seem to fix the problem for me. I recently installed the newest version of R and Rstudio and wondering if that might have something to do with the bug that I am seeing because it hadn't happened previously. The below reproduced the bug for me.

---
title: "Untitled"
output:
  pdf_document:
    keep_tex: true
    includes:
      in_header: ./header2.tex
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(dplyr)
library(kableExtra)
groups <- c(3, 3, 5, 4, 3, 3, 3, 2, 3, 2, 3)
groups <- setNames(groups, c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"))

x <- data.frame(Term = c('Other', 'White', 'Black', '18 to 24', '25 to 39', '40 to 59', 'Married', 'No longer married', 'Never married', 'Living with partner', 'Missing', '0-1', '2-5', '6-10', '11+', '0-1', '2-5', '6+', '0-1', '2-5', '6+', '0-1', '2-5', '6+', 'Yes', 'No', 'Never', 'Ever', 'Missing', 'Never', 'Ever', 'Heterosexual', 'Homosexual/Bisexual', 'Other'),
           uni = c('Ref', '0.87(0.36, 2.1)', '1.3(0.62, 2.7)^', 'Ref', '0.65(0.32, 1.3)', '0.69(0.38, 1.2)', 'Ref', '4.5(1.5, 13.5)**^', '3.8(1.6, 9.1)**^', '2.7(0.86, 8.3)^', '7.1(2.4, 20.7)***', 'Ref', '1.9(0.47, 7.3)^', '2.5(0.89, 6.9)^', '4.1(1.5, 11.6)**^', 'Ref', '4.2(2.2, 7.8)***', '14.1(2.6, 75.9)***^', 'Ref', '1.6(0.81, 3.1)^', '3.7(1.5, 9.2)**^', 'Ref', '2.8(1.2, 6.3)*^', '16.8(3.1, 92.5)***^', 'Ref', '0.59(0.28, 1.2)', 'Ref', '3.2(1.3, 7.9)*^', '6.6(3.3, 13.2)***', 'Ref', '2.7(1.5, 4.9)***', 'Ref', '1.4(0.48, 4.0)^', '0.69(0.16, 3.0)'),
           adj = c('Ref', '0.63(0.21, 1.9)', '0.86(0.40, 1.8)', 'Ref', '1.5(0.43, 5.1)^', '2.1(0.75, 5.9)^', 'Ref', '2.5(0.73, 8.5)^', '2.8(0.79, 9.6)^', '2.0(0.62, 6.4)^', '5.9(1.0, 34.4)^', 'Ref', '0.99(0.14, 7.1)', '0.70(0.16, 3.1)', '0.57(0.11, 3.0)', 'Ref', '3.3(0.87, 12.8)^', '4.2(0.85, 20.3)^', 'Ref', '1.3(0.44, 3.8)^', '2.1(0.47, 9.6)^', 'Ref', '0.65(0.13, 3.2)', '2.6(0.54, 12.5)^', 'NA', 'NA', 'Ref', '2.3(0.81, 6.5)^', '1.8(0.31, 11.1)^', 'Ref', '1.7(0.89, 3.1)^', 'NA', 'NA', 'NA'),
           uni1 = c('Ref', '1.1(0.37, 3.2)^', '0.67(0.19, 2.3)', 'Ref', '0.38(0.16, 0.89)*', '0.61(0.24, 1.5)', 'Ref', '3.5(1.0, 12.5)^', '2.4(0.84, 7.0)^', '1.7(0.34, 8.3)^', '8.0(2.5, 25.4)***', 'Ref', '1.2(0.23, 6.5)^', '2.5(0.81, 7.8)^', '4.1(1.3, 13.3)*^', 'Ref', '4.4(2.1, 9.3)***', '18.2(2.8, 120.2)***^', 'Ref', '2.9(0.96, 8.7)^', '6.0(1.9, 18.6)***^', 'Ref', '3.8(1.4, 10.4)*^', '22.5(3.2, 155.8)***^', 'Ref', '0.99(0.33, 3.0)', 'Ref', '4.8(1.8, 12.6)***^', '12.0(5.3, 27.1)***', 'Ref', '3.3(1.4, 7.9)*^', 'Ref', '0.98(0.24, 4.0)', '1.0(0.24, 4.6)^'),
           adj1 = c('Ref', '0.73(0.22, 2.5)', '0.47(0.14, 1.6)', 'Ref', '0.88(0.15, 5.2)', '1.9(0.48, 7.6)^', 'Ref', '1.7(0.48, 6.0)^', '1.8(0.39, 8.0)^', '1.2(0.28, 5.5)^', '6.5(0.95, 44.8)^', 'Ref', '0.36(0.06, 2.2)', '0.41(0.09, 1.8)', '0.36(0.08, 1.7)', 'Ref', '3.2(0.45, 23.2)^', '4.6(0.55, 38.9)^', 'Ref', '3.0(0.75, 12.0)^', '3.1(0.54, 18.1)^', 'Ref', '0.95(0.11, 8.0)', '3.3(0.41, 26.8)^', 'NA', 'NA', 'Ref', '3.3(1.3, 8.1)*^', '1.9(0.27, 13.1)^', 'Ref', '1.4(0.70, 2.9)^', 'NA', 'NA', 'NA'))
kable(x, format = "latex", longtable = T, booktabs = T, row.names = FALSE, linesep = "") %>%
  kable_styling(latex_options = c("striped")) %>% 
  group_rows(index = groups) 

and here is the header that I have

\PassOptionsToPackage{full}{textcomp} \usepackage{booktabs} \usepackage{longtable} \usepackage{array} \usepackage{multirow} \usepackage{wrapfig} \usepackage{float} \usepackage{colortbl} \usepackage{pdflscape} \usepackage{tabu} \usepackage{threeparttable} \usepackage{threeparttablex} \usepackage[normalem]{ulem} \usepackage{makecell} \usepackage{xcolor} \usepackage{xtab} \usepackage{setspace}

%% Language and font encodings \usepackage[english]{babel}

\usepackage[T1]{fontenc} \usepackage{setspace} \usepackage{newtxtext,newtxmath}

%% Useful packages \usepackage{amsmath} \usepackage{graphicx} \usepackage[colorinlistoftodos]{todonotes}

haozhu233 commented 5 years ago

Yeah, I also realized this recently. The last fix didn't entirely fix the issue.There are several reported issue that are related to this. I will try to get it fixed in the next version.

JamesCuster commented 5 years ago

Yea, I saw the other post, but I got the impression that those had been resolved. Thank you for the update though. Do you have a suggestion for a quick fix that I might be able to implement. The tables are for my thesis which I need to submit in a few weeks. If not, it is ok. For now I have just gone in and modified the raw LaTeX code which works fine, was just hoping to have an alternative to that.

Also, love the package by the way! I have been using it A LOT over the past year or so. So thank you for your work on it.