2013-11-11 5 views
7

Я пытаюсь иметь заголовки столбцов и горизонтальную линию под заголовками, разделенными группами. Когда я делаю следующее это работает,Hmisc отдельные заголовки столбцов с rowname = NULL

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

Кто-нибудь знает, как это исправить?

ответ

1

Функция латекса latex.default не использует аргумент группы столбцов, когда 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 file и 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)) 
} 

Заключительный сценарий:

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

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.

+0

@ тире Я посмотрел на github, это выглядит великолепно. – Cedric