adwish
adwish

Reputation: 13

PDF Rmarkdown - Improper Alignment in Multi-line Cells

For some reason, the following code generates a table in which the last column alternates alignment (left, right, center, left, ...). I've tried using \\ as a line breaker instead of \n but that only generated an awkward and large space between each feature. I would prefer if this last column is all centered - anyone have any ideas on why this is happening?

library(knitr)
library(kableExtra)
df <- data.frame(Metric = c("ECG (mV)", "EDA ($\\mu$s)","EMG (mV)", "Temp (C$^\\circ$)","Resp ($\\%$)", 
                            "BVP", "EDA ($\\mu$s)", "Temp (C$^\\circ$)"), 
                 Description = linebreak(c("Electrocardiography", "Electrodermal Activity", "Electromyography", "Body Temperature", "Respiration",
                                           "Blood Volume Pulse", "Electrodermal Activity", "Skin Temperature")), 
                 Features = linebreak(c("Mean \n Std Deviation \n Peaks", "Mean \n Std Deviation", "Mean \n Std Deviation", 
                                        "Mean \n Std Deviation", "Mean \n Std Deviation \n Peaks", 
                                        "Mean \n Std Deviation \n Heart Rate Variability", "Mean \n Std Deviation", 
                                        "Mean \n Std Deviation")))
kable(df, format = "latex", escape = F, align="c") %>%
  group_rows(index = c("RespiBAN Chest Sensor" = 5, "Empatica E4 Wrist Sensor" = 3)) %>%
  kable_styling(bootstrap_options = c("hover"))

table output

Upvotes: 1

Views: 223

Answers (2)

mnaR99
mnaR99

Reputation: 36

Adding align="c" to linebreak should work

library(knitr)
library(kableExtra)
df <- data.frame(Metric = c("ECG (mV)", "EDA ($\\mu$s)","EMG (mV)", "Temp (C$^\\circ$)","Resp ($\\%$)", 
                            "BVP", "EDA ($\\mu$s)", "Temp (C$^\\circ$)"), 
                 Description = linebreak(c("Electrocardiography", "Electrodermal Activity", "Electromyography", "Body Temperature", "Respiration",
                                           "Blood Volume Pulse", "Electrodermal Activity", "Skin Temperature")), 
                 Features = linebreak(c("Mean \n Std Deviation \n Peaks", "Mean \n Std Deviation", "Mean \n Std Deviation", 
                                        "Mean \n Std Deviation", "Mean \n Std Deviation \n Peaks", 
                                        "Mean \n Std Deviation \n Heart Rate Variability", "Mean \n Std Deviation", 
                                        "Mean \n Std Deviation"), align = "c")) #This parameter

Upvotes: 1

adwish
adwish

Reputation: 13

collapse_rows_dt <- data.frame(Metric = c(rep("ECG (mV)", 3), rep("EDA ($\\mu$s)", 2), rep("EMG (mV)", 2), rep("Temp (C$^\\circ$)", 2),
                                      rep("Resp ($\\%$)", 3), rep("BVP", 3), rep("EDA ($\\mu$s)", 2), rep("Temp (C$^\\circ$)", 2)),
                               Description = linebreak(c(rep("Electrocardiography", 3), 
                                                         rep("Electrodermal Activity", 2), 
                                                         rep("Electromyography", 2),
                                                         rep("Body Temperature", 2), 
                                                         rep("Respiration", 3), 
                                                         rep("Blood Volume Pulse", 3), 
                                                         rep("Electrodermal Activity", 2), 
                                                         rep("Skin Temperature", 2))),
                               Features = linebreak(c("Mean", "Std Deviation", "Peaks", 
                                        "Mean" , "Std Deviation", 
                                        "Mean", "Std Deviation", 
                                        "Mean", "Std Deviation", 
                                        "Mean",  "Std Deviation", "Peaks", 
                                        "Mean", "Std Deviation", "Heart Rate Variability", 
                                        "Mean", "Std Deviation", 
                                        "Mean", "Std Deviation")))
kable(collapse_rows_dt, escape=F, booktabs = T, align = "llc") %>%
  group_rows(index = c("RespiBAN Chest Sensor" = 12, "Empatica E4 Wrist Sensor" = 7)) %>%
  column_spec(1, bold=T) %>%
  collapse_rows(columns = 1:2, latex_hline = "major", valign = "middle")

Definitely not an ideal solution, but it did the job

centered table

Upvotes: 0

Related Questions