Hmisc使用rowname = NULL分隔列标题

时间:2013-11-11 00:01:33

标签: r hmisc

我正在尝试将列标题和标题下方的水平线分隔。当我执行以下操作时,它可以正常工作,

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

enter image description here

但是当我尝试删除rownames时,第1组& 2合并到同一行

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

enter image description here

有没有人知道解决这个问题的方法?

2 个答案:

答案 0 :(得分:2)

使用我自己的huxtable包的一种方式:

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

这有一个限制:你不能得到漂亮的双线。但是如果这是一个足够的替代品,你可以改变线宽。您也可以尝试pixiedust包。

答案 1 :(得分:1)

latex.default时,latex函数rownames=NULL不使用列组参数。在我们的功能中:

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
}

将cgroup.cols作为从n.cgroup参数

计算的第一列和最后一列
     first.col last.col
[1,]         1        5
[2,]         7       12

我认为这是一个错误。因此,目前唯一的方法是使用此功能手动编辑您的tex输出,在该功能中,您重复传递给乳胶的参数,以获取tex filen.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))
}

最终剧本:

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

生成正确的格式

correct_format