使用Kable将包装文本的列名称居中

问题描述 投票:0回答:1

我在列出包装文本的列名时遇到了一些麻烦。包装文本的顶行居中,但第二行没有。

test_data <- data.frame(Mean = runif(5, 3.71, 7.72),
                        N = sample(57:59, 5, replace = TRUE),
                        sd = c(1, rep(0, 4)),
                        d = rep(1, 5),
                        naod = sample(1:4, 5, replace = TRUE),
                        a = sample(5:12, 5, replace = TRUE),
                        sa = sample(37:44, 5, replace = TRUE)
test <-as.data.frame(t(as.matrix(sapply(2:6,function(i) vec_fun5(test_Data,i)))))

kable(test,"latex" ,booktabs=T, align="c",col.names=linebreak(c('Mean','\\textit{N}' ,'Strongly\n Disagree','Disagree','Neither Agree\n or Disagree','Agree','Strongly\n Agree')),row.names = T,escape=F)%>%
  row_spec(0,align = "c")

Output Table

我想让两条线都在细胞内居中。

r formatting centering kable columnname
1个回答
2
投票

您可以使用tableHTML

测试数据:

set.seed(1)
test_data <- data.frame(Mean = runif(5, 3.71, 7.72),
                        N = sample(57:59, 5, replace = TRUE),
                        sd = c(1, rep(0, 4)),
                        d = rep(1, 5),
                        naod = sample(1:4, 5, replace = TRUE),
                        a = sample(5:12, 5, replace = TRUE),
                        sa = sample(37:44, 5, replace = TRUE))

library(tableHTML)



test_data %>% 
  tableHTML(round = 2, 
            widths = c(50, 50, 50, 50, 
                       80, 120, 50, 50),
            headers = c("Mean", "N",
                        "Strongly <br>Disagree",
                        "Disagree",
                        "Neither Agree <br> or Disagree",
                        "Agree",
                        "Strongly <br>Agree"),
            escape = FALSE) %>% 
  add_theme("scientific")

结果如下:

output

© www.soinside.com 2019 - 2024. All rights reserved.