1

I want to produce a number of tables for the same set of variables. I use the wonderful R package compareGroups and Hmisc to assign variable labels so that I don't have to change variable names for each table. Some variable names include latex math mode but this gives me an error because compareGroups exports $that$ like this: \$that\$. Here is a minimal example:

# assigning Hmisc label includinc math mode in $$
library(Hmisc)
label(mtcars$mpg) <- "[$\\frac{miles}{gallon}$]"

# descriptive table of mpg by am
library(compareGroups)

tab <- createTable(
  compareGroups(data = mtcars, 
              am ~ mpg)
  )

# in createTable, the variable name looks fine
tab

# when exported to latex, a \ is inserted before each $
export2latex(tab)

Is there a way to avoid that a \ is inserted before $ by export2latex? Or any workaround?

ehi
  • 409
  • 8
  • 23
  • I'm afraid that appears to be hardcoded into `export2latex.createTable`. There doesn't appear to be an option to turn it off. – Benjamin Jan 11 '17 at 11:44

2 Answers2

2

Here's a workaround that uses pixiedust to generate the table. pixiedust has an option to "sanitize" the content of a cell via Hmisc::latexTranslate. By default, this option is turned off.

Solution is given as an Rmd file

---
title: "Untitled"
output: pdf_document
header-includes: 
- \usepackage{amssymb} 
- \usepackage{arydshln} 
- \usepackage{caption} 
- \usepackage{graphicx} 
- \usepackage{hhline} 
- \usepackage{longtable} 
- \usepackage{multirow} 
- \usepackage[dvipsnames,table]{xcolor} 
---

```{r, include = FALSE}
library(Hmisc)
library(compareGroups)
library(broom)
library(magrittr)
library(pixiedust)
```

```{r}
label(mtcars$mpg) <- "[$\\frac{miles}{gallon}$]"

# descriptive table of mpg by am
tab <- createTable(
  compareGroups(data = mtcars, 
                gear ~ mpg + qsec)
)

tab_df <- 
  tab$descr %>%
  tidy()

tab_head <- 
  c("", attr(tab, "ylevels"), "p.overall",
    "", sprintf("N=%s", tab$avail[1, -c(1, (ncol(tab$avail) - 0:1))]), "") %>%
  matrix(nrow = 2, 
         byrow = TRUE) %>%
  tidy %>%
  setNames(names(tab_df))

dust(tab_df,
     float = FALSE) %>%
  redust(tab_head, part = "head") %>%
  medley_bw()
```

enter image description here

If you want to make it a touch easier, you can extend the dust method to include a dust.createTable method (this is primitive, and I haven't thought through all the use cases, but for example)

dust.createTable <- function(object, ...){
  obj_df <- 
    object$descr %>%
    broom::tidy()

  obj_head <- 
    c("", attr(object, "ylevels"), "p.overall",
      "", sprintf("N=%s", object$avail[1, -c(1, (ncol(object$avail) - 0:1))]), "") %>%
    matrix(nrow = 2, 
           byrow = TRUE) %>%
    broom::tidy() %>%
    stats::setNames(names(obj_df))

  pixiedust::dust(obj_df) %>%
    pixiedust::redust(obj_head, part = "head")
}


compareGroups(data = mtcars,
            gear ~ mpg + qsec) %>%
  createTable() %>%
  dust()

compareGroups(data = mtcars,
              gear ~ mpg + qsec + wt) %>%
  createTable() %>%
  dust()
Benjamin
  • 16,897
  • 6
  • 45
  • 65
0

I received a message from compareGroups forum (link) withan easy solution. You could also just substitute "\$" with "$" using sub function:

tab <- export2latex(tab)
sub("\\$","$", 
    tab, 
    fixed=TRUE, 
    perl=FALSE)
ehi
  • 409
  • 8
  • 23