首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Kable将换行文本的列名居中

问使用Kable将换行文本的列名居中
EN

Stack Overflow用户
提问于 2019-04-04 21:29:57
回答 1查看 109关注 0票数 0

我在居中显示有换行文本的列名时遇到了一些问题。换行文本的第一行居中,但第二行不居中。

代码语言:javascript
复制
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

我想让两条线都在单元格内居中。

EN

回答 1

Stack Overflow用户

发布于 2019-04-04 22:16:35

为此,您可以使用tableHTML:

测试数据:

代码语言:javascript
复制
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")

结果如下所示:

​

​

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55517245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档