0

I'm using the Hmisc package in R to generate a reproducible LaTex table and would like to suppress some of the horizontal lines (\midrule) generated by default in the booktabs mode. I've sifted through the Hmisc documentation as well as a number of Hmisc solutions here and on tex.stackexchange and I don't believe anything like this has been answered yet (but please correct me if I'm wrong).

Here's a minimal example that should be reproducible:

library(Hmisc)    
myDF <- data.frame("foo" = c(1:10),
                       "bar" = rep(c("a","b"),5),
                       "baz" = c(21:30))

latex(myDF,
      file="",
      rowname = "",
      rowlabel = "",
      rgroup = c("Group A", "Group B", "Group C"),
      n.rgroup = c(3, 4, 3),
      booktabs = TRUE
      )

Which generates the following output:

 %latex.default(myDF, file = "", rowname = "", rowlabel = "", rgroup = c("Group A",     "Group B", "Group C"), n.rgroup = c(3, 4, 3), booktabs = TRUE,     )%
\begin{table}[!tbp]
\begin{center}
\begin{tabular}{lrlr}
\toprule
\multicolumn{1}{l}{}&\multicolumn{1}{c}{foo}&\multicolumn{1}{c}{bar}&\multicolumn{1}{c}{baz}\tabularnewline
\midrule
{\bfseries Group A}&&&\tabularnewline
~~&$ 1$&a&$21$\tabularnewline
~~&$ 2$&b&$22$\tabularnewline
~~&$ 3$&a&$23$\tabularnewline
\midrule
{\bfseries Group B}&&&\tabularnewline
~~&$ 4$&b&$24$\tabularnewline
~~&$ 5$&a&$25$\tabularnewline
~~&$ 6$&b&$26$\tabularnewline
~~&$ 7$&a&$27$\tabularnewline
\midrule
{\bfseries Group C}&&&\tabularnewline
~~&$ 8$&b&$28$\tabularnewline
~~&$ 9$&a&$29$\tabularnewline
~~&$10$&b&$30$\tabularnewline
\bottomrule
\end{tabular}\end{center}
\end{table}

How can I suppress some or all occurrences of '\midrule' in the output?

Please note that while I'm not attached to solving this with Hmisc, I have some constraints that the rgroup flag solves in a very effective way. While I do not believe that xtable or stargazer (for example) provide usable alternatives, I'm certainly open to other packages as long as I can preserve a layout with row groupings as provided in my example.

ashaw
  • 198
  • 5

1 Answers1

2

The approach I've taken to dealing with Hmisc::latex is to get it as close as I can, capture the results, fix it with regex, and then print. Using regex here makes me feel mildly guilty, as I suspect there's a purpose-built way to do these things, but after a couple hours of searching I just don't care anymore.

In RMarkdown,

---
title: "Table without midrules"
author: "Edward Visel"
date: "12/3/2016"
header-includes:
   - \usepackage{booktabs}
output: pdf_document
---


```{r table, message=FALSE, results='asis'}
library(Hmisc)    
myDF <- data.frame("foo" = c(1:10),
                       "bar" = rep(c("a","b"),5),
                       "baz" = c(21:30))

tb <- capture.output(latex(myDF,
      file="",
      rowname = "",
      rowlabel = "",
      rgroup = c("Group A", "Group B", "Group C"),
      n.rgroup = c(3, 4, 3),
      booktabs = TRUE, 
      where = '!htbp'    # so table prints in correct location
      ))[-1]    # so LaTeX comment at beginning doesn't print

cat(grep('\\midrule', tb, value = TRUE, invert = TRUE))    # use gsub if you prefer
```

table without midrules

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • Thanks, @alistaire! This definitely solves the immediate problem. I was sorting out a similar hack when you posted your response. I'll leave the question open for a bit longer just in case someone comes along with a native `Hmisc` solution. – ashaw Dec 04 '16 at 00:00