Hmisc separate column headings with rowname = NULL

I'm trying to have column headings and the horizontal line underneath the headings separated by the groups. When I do the following it works,

library(Hmisc)
data(mtcars)
latex(mtcars, file ='', cgroup = c("Group 1", "Group 2"), n.cgroup = c(5, 6))

在这里输入图像描述

but when I try to remove the rownames, the line under Group 1 & 2 merge into the same line

library(Hmisc)
data(mtcars)
latex(mtcars, file ='', cgroup = c("Group 1", "Group 2"), n.cgroup = c(5, 6), rowname = NULL)

在这里输入图像描述

Does anyone know of a way to fix this?


The latex function latex.default does not use the column group argument when rownames=NULL . In the function we have :

if (length(rowname)) {
    cx <- cbind(rowname, cx)
    col.just <- c(rowlabel.just, col.just)
    if (length(extracolheads)) 
        extracolheads <- c("", extracolheads)
    collabel.just <- c(rowlabel.just, collabel.just)
    if (length(cgroup) == 0L) 
        colheads <- c(rowlabel, colheads)
    else {
        colheads <- c("", colheads)
        cgroup <- c(rowlabel, cgroup)
        rlj <- ifelse(rowlabel.just == "l", "l", "c")
        cgroup.just <- c(rlj, cgroup.just)
        n.cgroup <- c(1, n.cgroup)
        cgroup.cols <- 1 + cgroup.cols
        cline <- paste(sl, "cline{", cgroup.cols[, 1], "-", cgroup.cols[, 
            2], "}", sep = "", collapse = " ")
    }
    nc <- 1 + nc
}

with cgroup.cols as the first and last columns computed from the n.cgroup argument

     first.col last.col
[1,]         1        5
[2,]         7       12

I think it's a bug. So currently the only way to go is to edit your tex output manually, using this function, in which your repeat the arguments that you passed to your latex for the name of your tex file and n.cgroup :

change_cline <- function(file,n.cgroup){
  file_to_edit <-readLines(file)
  pos <- grep("cline",file_to_edit) # get the position of cline in the tex file
  cgroup.cols <- matrix(c(1,n.cgroup[2]+1,n.cgroup[1],sum(n.cgroup)+1), nrow=2) 
  file_to_edit[pos]<- paste("cline{", cgroup.cols[, 1], "-", cgroup.cols[, 
          2], "}", sep = "", collapse = " ")
  cat(file_to_edit, file = file,sep="n")
  return(invisible(NULL))
}

Final script :

data(mtcars)
la<-latex(mtcars, file ='mtcars2.tex', cgroup = c("Group 1", "Group 2"), n.cgroup = c(5, 6), rowname = NULL)
change_cline(file='mtcars2.tex',n.cgroup = c(5 ,6))

which produces the correct format

correct_format


One way using my own huxtable package:

library(huxtable)
mt_hux <- as_huxtable(mtcars, add_rownames = TRUE, add_colnames = TRUE)
mt_hux <- insert_column(mt_hux, rep("", nrow(mt_hux)), after = 6)
mt_hux <- insert_row(mt_hux, c("", "Group 1", "", "", "", "", "", "Group 2", "", "", "", "", ""))
mt_hux[2, 1] <- "" # get rid of ugly 'row names'

colspan(mt_hux)[1, c(2, 8)] <- c(5, 6)
align(mt_hux)[1, c(2, 8)] <- "center"
top_border(mt_hux)[1,] <- 1
bottom_border(mt_hux)[1, c(2, 8)] <- 1
bottom_border(mt_hux)[2,] <- 1

# this should be what you want:
mt_hux

This has a limitation: you can't get the nice double line. But you can vary the line width, if that is an adequate substitute. You could also try the pixiedust package.

链接地址: http://www.djcxy.com/p/17254.html

上一篇: var functionName = function(){} vs function functionName(){}

下一篇: Hmisc使用rowname = NULL分隔列标题