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

Ale*_*sen 1 alignment knitr kableextra

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

未使用的参数(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)
Run Code Online (Sandbox Code Playgroud)

Mic*_*per 7

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

*列对齐:由“l”(左)、“c”(居中)和/或“r”(右)组成的字符向量。默认情况下或如果 align = NULL,数字列右对齐,其他列左对齐。如果length(align) == 1L,则字符串将扩展为单个字母的向量,例如'clc' 变为c('c', 'l', 'c'),除非输出格式是LaTeX。

作为一些例子:

knitr::kable(mtcars[1:5, 1:5], caption = "Left Aligned")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

knitr::kable(mtcars[1:5, 1:5], align = "c", caption = "Center Aligned")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

在此处输入图片说明