是否可以使用不带kableExtra的kable更改列宽? [英] Is it possible to change the column width using kable without kableExtra?

查看:306
本文介绍了是否可以使用不带kableExtra的kable更改列宽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题.我想修改结果的列宽,但是如果不使用kableExtra则无法完成.如果加载kableExtra包,则图像将变为文本格式.

Continue from this question. I want to modify the result's column width, but it can't be done without using the kableExtra. If I load the kableExtra package, the image will become text format.

代码如下:

---
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,我找不到改变宽度的任何方法.我是否缺少可以解决我的问题的东西?

I can't find any way to change the width without using kableExtra. Am I missing something that can solve my problem?

非常感谢.

推荐答案

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

If you have raw HTML or LaTeX code in the data, remember to put escape = FALSE in kable() to avoid escaping of special characters.

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

这篇关于是否可以使用不带kableExtra的kable更改列宽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆