Вертикальное выравнивание в kableextra

У меня проблемы с выравниванием результатов в Kableextra.

---
title: "Report test 1"
author: "Adam"
date: "May 9, 2019"
output: pdf_document
---

COLLEGE: College of Education

DEPARTMENT: Office of Assessment and Accreditation

SEMESTER: Fall 2018

SECTION 1:  Please provide the mean and frequency distribution of the two required questions for all Traditional Delivery Courses. NOTE:  The MEAN and N is required. The Frequency is optional.  

x<-c(45,2,8,10,32,33)
y<-c(2,3,3,3,3,3)
EDLP_D.1<-cbind(x,y)

colnames(EDLP_D.1)<- c("Sheet", "Please indicate your level of satisfaction with the availability of the instructor outside the classroom. (In selecting your rating, consider the instructor's availability via established office hours, appointments, and opportunities for face-to-face interaction as well and as via telephones, e-mail, fax, and other means") 

#function to compute values needed for table 
vec_fun4 <- function(x,y){
  Vec <-c(colnames(x[,y]),round(mean(x[[y]]),2),length(which(!is.na(x[,y]==F))),length(which(x[,y]==1)),length(which(x[,y]==2)),length(which(x[,y]==3)),length(which(x[,y]==4)))
  return(Vec)   
}

#Switch from long format to wide format
item2.1 <- as.data.frame(t(as.matrix(vec_fun4(EDLP_D.1,1))))

#Make table
library(kableExtra)

kable(item2.1,"latex" ,booktabs=T, align = "lcccccc", col.names=linebreak(c(' ','Mean','\\textit{N}' ,'Strongly\n Disagree','Disagree','Agree','Strongly\n Agree')),row.names = F,escape=F) %>% 
  column_spec(1, width = "20em" )

Я хотел бы, чтобы числовые значения центрировались по горизонтали и вертикали в их ячейках.

Я приложил копию таблицы здесь

Таблица


person asokol    schedule 09.05.2019    source источник


Ответы (2)


У меня недостаточно очков, чтобы оставить комментарий. У меня такая же проблема, вы когда-нибудь в этом разбирались?

Вы можете попробовать эту функцию для центрирования текста:

centerText <- function(text){
    paste0("\\multirow{1}{*}[0pt]{", text, "}")
  }

Однако это может испортить ваше форматирование, такое как ширина столбца и перенос текста, как это было у меня. Пожалуйста, дайте мне знать, если найдете лучшее решение.

person David    schedule 25.11.2019
comment
Я еще не разобрался. - person asokol; 16.12.2019

Мое решение - заменить несколько латексных скриптов

пример:

```{r results='asis'}
library(kableExtra)
library(dplyr)
library(stringr)
kable(
  data.frame(
    'x1' = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reprehenderit voluptates ut quam delectus perferendis tempora, voluptas fuga deleniti asperiores ipsam ipsum nam animi sequi, nisi dicta, minus pariatur atque, nemo.',
    'x2' = 11,
    'x3' = 22,
    'x4' = 33
  ),
  # align = 'lccr',
  # booktabs = T
  col.names = c(' ','X2','X3' ,'X4'),
  format = "latex"
) %>% 
  # column_spec(1, width = "20em") %>%
  # column_spec(2:4, width = "5em") %>%
  str_replace(
    '(?<=\\\\begin\\{tabular\\}).+',
    '{ >{\\\\raggedright\\\\arraybackslash}m{20em} >{\\\\centering\\\\arraybackslash}m{5em} >{\\\\centering\\\\arraybackslash}m{5em} >{\\\\raggedleft\\\\arraybackslash}m{5em} }'
  ) %>%
  cat
```

введите здесь описание изображения

ref: https://es.overleaf.com/learn/latex/Tables

person Ali    schedule 17.01.2020