kableExtra :如何将行的最大值设置为粗体? [英] kableExtra : How can i set to bold the biggest value of the row?

查看:54
本文介绍了kableExtra :如何将行的最大值设置为粗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个看起来像这样的表格:

Suppose i have a table that looks like :

x = matrix(runif(10*5),nrow=10,ncol=5)

当我使用 kableextra 显示矩阵时,我希望 每行的最高值,例如最后 2 行,加粗.

When i display the matrix using kableextra, i want the highest value, per row, of say the last 2 rows, to be bolded.

我看了这个文档https://rdrr.io/cran/kableExtra/f/inst/doc/awesome_table_in_pdf.pdf 很多,但我没有找到如何正确使用 cell_spec 来执行此目标.

I looked at this document https://rdrr.io/cran/kableExtra/f/inst/doc/awesome_table_in_pdf.pdf a lot and i did not found how to use cell_spec correctly to perform this goal.

推荐答案

我认为这会比结果更容易.据我所知,这是如何做到的:

I thought this would be easier than it turned out to be. As far as I can see, this is how to do it:

---
title: "Untitled"
output:  pdf_document
---

```{r}
set.seed(123)
library(knitr)
library(kableExtra)
x <- matrix(round(runif(10*5),2), nrow=10,ncol=5)
j1 <- which.max(x[9,])
j2 <- which.max(x[10,])
col <- seq_len(ncol(x))
x[9,] <- x[9,] %>% cell_spec(bold = col == j1)
x[10,] <- x[10,] %>% cell_spec(bold = col == j2)
x %>% kable(booktabs = TRUE, escape = FALSE)
```

一些注意事项:

  • 我对这些值进行了四舍五入,因此打印时它们不会那么难看.
  • 我看不到一种方法可以在一个管道中完成所有工作,尽管可能有一个.问题在于 cell_spec 旨在处理向量,而不是矩阵.
  • 最后,kable() 中的 escape = FALSE 是必不可少的:否则您将看到代码使其变为粗体,而不是粗体条目本身.
  • I rounded the values so they aren't so ugly when printed.
  • I couldn't see a way to do everything in one pipeline, though there probably is one. The trouble is that cell_spec is designed to work on vectors, not matrices.
  • Finally, the escape = FALSE in kable() is essential: otherwise you'll see the code to make it bold, rather than the bold entry itself.

这篇关于kableExtra :如何将行的最大值设置为粗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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