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

时间:2017-12-19 14:14:34

标签: r knitr kableextra kable

首先,包kableExtra确实产生了很棒的表。我还有一个额外的请求:

运行此示例时(来自手册)

 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") 

cell_spec

在每列中形成细胞。 (1)如何将它们调整到整个桌子? (2)或用户定义的比例?我注意到kableExtra::spec_color和。{ kableExtra::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}

2 个答案:

答案 0 :(得分:2)

好的,使用最新的开发者,您可以在scale_fromspec_color&amp;中使用spec_font_sizespec_angle

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") 

enter image description here

答案 1 :(得分:0)

很抱歉问一下,使用语法a give

评估错误:未使用的参数(scale_from = range(unlist(iris [1:10,])))。 电话:... as.data.frame - &gt;变异 - &gt; mutate.tbl_df - &gt; mutate_impl - &gt; .CALL

执行暂停