kableExtra 如何使用用户定义范围内的 cell_spec?

kableExtra how to use cell_spec from a user defined range?

首先,包 kableExtra 确实产生了很棒的 table。我还有一个要求:

当运行这个例子(来自手册)

 iris[1:10, ] %>% select(-Species) %>%
    mutate_if(is.numeric, function(x) {
          cell_spec(x, "latex", bold = T, color = spec_color(x, end = 0.9),
              font_size = spec_font_size(x))
        }) %>%
    kable("latex", escape = F, booktabs = T, linesep = "", align = "c") 

单元格按每列格式排列。 (1) 如何将它们调整到整个 table ? (2) 或者用户定义的比例?我注意到 kableExtra::spec_colorkableExtra::cell_spec 使用 scales::rescale 函数,该函数确实带有 from 参数,但未在函数中使用。

下面是可重现的 .Rnw 脚本的代码。

\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} 
\usepackage{booktabs}
\usepackage{float}
\usepackage{colortbl}

\begin{document}
<<tab10, echo=FALSE, results="as.is">>=
require(knitr)
require(kableExtra)
require(tidyverse)
@
<<tab1, echo=FALSE, results="as.is">>=
require(knitr)
require(kableExtra)
require(tidyverse)
iris[1:10, ] %>% select(-Species) %>%
    mutate_if(is.numeric, function(x) {
          cell_spec(x, "latex", bold = T, color = spec_color(x, end = 0.9),
              font_size = spec_font_size(x))
        }) %>%
    kable("latex", escape = F, booktabs = T, linesep = "", align = "c") 
@


\end{document}

好的,使用最新的开发版本,您可以在spec_colorspec_font_sizespec_angle中使用scale_from

library(kableExtra)
library(dplyr)
library(knitr)
iris[1:10, ] %>% 
  select(-Species) %>%
  mutate_if(is.numeric, function(x) {
    cell_spec(x, "latex", bold = T, 
              color = spec_color(x, end = 0.9, scale_from = range(unlist(iris[1:10, ]))),
              font_size = spec_font_size(x, scale_from = range(unlist(iris[1:10, ]))))
  }) %>%
  kable("latex", escape = F, booktabs = T, linesep = "", align = "c")