kableExtra how to use cell_spec from a user defined range?

这一生的挚爱 提交于 2021-02-17 03:28:27

问题


First, the package kableExtra indeed produces awesome table. I just have an additional request :

When running this example ( from the manual)

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

The cells are formated in each column. (1) How to adjust them to the entire table ? (2) Or to a user defined scale ? I noticed that both kableExtra::spec_color and kableExtra::cell_spec make use of the scales::rescale function which does take a from argument but it's not used in the functions.

Below code of reproducible .Rnw script.

\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}

回答1:


Okay, with the latest dev ver, you can use scale_from in spec_color, spec_font_size & spec_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") 




回答2:


Sorry for ask, a use the syntax a give

Evaluation error: unused argument (scale_from = range(unlist(iris[1:10, ]))). Calls: ... as.data.frame -> mutate -> mutate.tbl_df -> mutate_impl -> .Call

Execution halted



来源:https://stackoverflow.com/questions/47888885/kableextra-how-to-use-cell-spec-from-a-user-defined-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!