是否可以在没有 kableExtra 的情况下使用 kable 更改列宽?

Is it possible to change the column width using kable without kableExtra?

继续。我想修改结果的列宽,但是不使用 kableExtra 是做不到的。如果我加载 kableExtra 包,图像将变成文本格式。

代码如下:

---
title: "Untitled"
output: 
    pdf_document:
        latex_engine: xelatex
---
```{r echo=FALSE, results='hide', warning=FALSE, message=FALSE}
## Load modules
library(dplyr)
library(tidyr)
library(ggplot2)
options(tinytex.verbose = TRUE)
## Create a local function to plot the z score
varianceChart <- function(df, personNumber) {
  plot <- df %>%
         filter(n == personNumber) %>%
         ggplot() +
         aes(x=zscore, y=0) +
         geom_rect(aes(xmin=-3.32, xmax=-1.96, ymin=-1, ymax=1), fill="orange2", alpha=0.8) + 
         geom_rect(aes(xmin=1.96, xmax=3.32, ymin=-1, ymax=1), fill="olivedrab3", alpha=0.8) +
         geom_rect(aes(xmin=min(-4, zscore), xmax=-3.32, ymin=-1, ymax=1), fill="orangered3") + 
         geom_rect(aes(xmin=3.32, xmax=max(4, zscore), ymin=-1, ymax=1), fill="chartreuse4") +
         theme(axis.title = element_blank(), 
               axis.ticks = element_blank(), 
               axis.text = element_blank(),
               panel.grid.minor = element_blank(),
               panel.grid.major = element_blank()) +
               geom_vline(xintercept=0, colour="black", alpha=0.3) +
               geom_point(size=15, shape=4, fill="lightblue") ##Cross looks better than diamond
  return(plot)
}

## Create dummy data
Person1 <- rnorm(1, mean=10, sd=2) 
Person2 <- rnorm(1, mean=10, sd=2)
Person3 <- rnorm(1, mean=10, sd=2)
Person4 <- rnorm(1, mean=10, sd=2) 
Person5 <- rnorm(1, mean=10, sd=2) 
Person6 <- rnorm(1, mean=6,  sd=1) 

## Add to data frame
df <- data.frame(Person1, Person2, Person3, Person4, Person5, Person6)

## Bring all samples into one column and then calculate stats
df2  <- df %>% gather(key=Person, value=time)
mean <- mean(df2$time)
sd   <- sqrt(var(df2$time))

stats <- df2 %>%
             mutate(n = row_number()) %>%
             group_by(Person) %>%
             mutate(zscore = (time - mean) / sd)

graph_directory <- getwd() #'./Graphs'

## Now to cycle through each Person and create a graph
for(i in seq(1, nrow(stats))) {
  print(i)
  varianceChart(stats, i)

  ggsave(sprintf("%s/%s.png", graph_directory, i), plot=last_plot(), units="mm", width=100, height=20, dpi=1200)
}

## add a markup reference to this dataframe
stats$varianceChart <- sprintf('\raisebox{-.4\totalheight}{\includegraphics[width=0.2\textwidth, height=20mm]{%s/%s.png}}', graph_directory, stats$n) 

df.table <- stats[, c(1,2,5)]
colnames(df.table) <- c("PersonName", "Timetaken", "VarianceChart")
df.table$note=paste0("This is a note that contaions a lot of strings ",paste0(LETTERS,collapse = ", "))
```

```{r}
library(knitr)
kable(df.table, caption="The last column is too long that can't be shown in single line.")
```

```{r}
library(kableExtra)
kable(df.table,format = "latex") %>%
  kable_styling(full_width=F) %>%
  column_spec(4, width = "4em")
```

两个结果是

我找不到任何不使用 kableExtra 来改变宽度的方法。我是否缺少可以解决我问题的东西?

非常感谢。

如果数据中有原始 HTML 或 LaTeX 代码,请记住将 escape = FALSE 放在 kable() 中以避免转义特殊字符。

```{r}
library(kableExtra)
kable(df.table, "latex", booktabs = TRUE, escape = FALSE) %>%
  kable_styling(full_width = FALSE) %>%
  column_spec(4, width = "18em")
```