asokol
asokol

Reputation: 129

Centering column names with wrapped text with Kable

I am having some trouble centering column names that have wrapped text. The top line of the wrapped text centers but the second line does not.

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

I would like to have both lines centered within the cell.

Upvotes: 0

Views: 258

Answers (1)

clemens
clemens

Reputation: 6813

You could use tableHTML for that:

Test data:

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")

The result looks like this:

output

Upvotes: 2

Related Questions