如何在 knitr 中对齐列标题和内容

how to align column title and content in knitr

只是试图将列的标题与其在 knitr 中的内容对齐。什么是功能?当我使用 align = 'c'position = 'c' 时,它给我一个错误:

unused argument (align = "c").

这是我的代码:

DF_3 <-structure(list(. = structure(c(11L, 6L, 7L, 8L, 13L, 5L, 4L, 2L, 12L, 3L, 1L, 10L, 9L),
                                    .Label = c("B", "D", "E", "F", "G", "H", "H", "H", "I", "J", "M", "N", "V"), class = "f"),
                      HEV.pos = structure(c(3L, 8L, 1L, 6L, 2L, 9L, 8L, 10L, 7L, 3L, 8L, 4L, 5L),
                                          .Label = c("1 (2)", "15", "16", "19", "20", "24", "26", "4 (7)", "6 (11)", "7 (13)"), class = "factor"),
                      HEV.neg = structure(c(9L, 2L, 7L, 1L, 6L, 1L, 7L, 10L, 5L, 4L, 8L, 3L, 5L),
                                          .Label = c("10 (28)", "12 ", "15 ", "17", "18 ", "19 ", "2 ", "4", "7", "9"), class = "factor")),
                 class = "data.frame", row.names = c(NA, -13L)) 


library(knitr)
library(kableExtra)
library(dplyr)

kable(DF_3, format = "html") %>% 
  kable_styling(bootstrap_options = "striped") %>%
  row_spec(1, bold = T) %>% 
  row_spec(6, bold = T) %>%
  column_spec(1, bold = T)

不清楚您在何处尝试使用 align 参数。但是,它必须提供给 kable 函数。函数文档统计:

*Column alignment: a character vector consisting of 'l' (left), 'c' (center) and/or 'r' (right). By default or if align = NULL, numeric columns are right-aligned, and other columns are left-aligned. If length(align) == 1L, the string will be expanded to a vector of individual letters, e.g. 'clc' becomes c('c', 'l', 'c'), unless the output format is LaTeX.

举几个例子:

knitr::kable(mtcars[1:5, 1:5], caption = "Left Aligned")

knitr::kable(mtcars[1:5, 1:5], align = "c", caption = "Center Aligned")

knitr::kable(mtcars[1:5, 1:5], align = c("c", "l", "r", "c", "c"), caption = "Mixed Aligned")