[我正在重新发明轮子(*),它的形状问题浮现在脑海。]

对于graphical display of a table,我想提出一个主题,该主题可让您轻松访问所有美学定制(例如,为单元格填充背景,文本颜色,字体大小等)。

列表是显而易见的选择。但是,复杂的原因在于表也具有内部结构。它可以具有列标题,行标题以及核心条目。这三个模块中的每个模块都应具有自己特定的美学默认值和要求,但要继承一个共同的父对象。确实,我希望能够直接指定所有字体都应为黑色,除非我专门覆盖列标题,否则它也将继承此颜色。

一种可能的构造是R中现有的函数环境(闭包)框架:评估参数时,嵌套结构会自动解析。这是我的示例:

# helping function
inplace <- function(l, ...) modifyList(l, list(...))

table_theme <- function(bg = c("grey95", "grey98"),
                        fg = c("black", "black"),
                        just=c("center","center"), hjust=0.5, vjust=0.5,
                        padding = unit(c(4,4),"mm"),
                        font = list(family = "sans", size = 12, face = "plain"),
                        separator = list(h=FALSE, v=TRUE), box = FALSE,
                        core = list(bg=bg, fg=fg, parse=TRUE,
                                    separator=separator, box=box, padding=padding,
                                    just=just, hjust=hjust, vjust=vjust, font=font),
                        col_header = inplace(core, parse=FALSE,
                                             font=inplace(font, face="bold")),
                        row_header = inplace(col_header, just=c("right", "center"),
                                             font = inplace(font, face="italic"),
                                             hjust=1, padding=unit(c(4,4),"mm"))){

  list(bg=bg, fg=fg,
       font=font, separator=separator, box=box,
       core=core, row_header=row_header, col_header=col_header)
}

theme = table_theme()

它工作正常(令人惊讶!),但是仍然存在一个问题:如果我只想更改子项的一个默认属性,那么我将丢失所有其他默认值。例如,在上面的theme中,在构造过程中修改theme$col_header$font并非易事:我必须将完整的调用重写为
table_theme(col_header = inplace(list(bg = c("grey95", "grey98"),
                            fg = c("black", "black"),
                            just=c("center","center"), hjust=0.5, vjust=0.5,
                            padding = unit(c(4,4),"mm"),
                            font = list(family = "sans", size = 12, face = "plain"),
                            separator = list(h=FALSE, v=TRUE), box = FALSE),
             font=inplace(font, face="bold"))

但是,在创建主题之后,修改相对容易,
theme$col_header$font$face <- "bold"

我是否错过了一种巧妙地利用两全其美的方法-使用封闭结构并轻松获取孩子的各个特征来对嵌套结构进行自动自洽评估?

PS:
(*):我知道ggplot2有一个具有某些继承特性的新主题系统;不幸的是,它与ggplot2的内部渲染系统和命名系统紧密地绑定(bind)在一起,因此对于其他图形元素而言相当不可用。

最佳答案

与其重新发明轮子,不如从点阵包的参数设置实用程序功能中汲取灵感。要修改晶格的图形参数树(由trellis.par.get()返回,trellis.par.set())的各个叶子,它允许您传递仅包含要更改的元素值的列表结构。

除了继续讨论之外,我仅向您展示将该想法应用于您的问题的方式:

library(grid)
inplace <- function(l, ...) modifyList(l, list(...))
table_theme <-
    function(...,
             bg = c("grey95", "grey98"),
             fg = c("black", "black"),
             just=c("center","center"), hjust=0.5, vjust=0.5,
             padding = unit(c(4,4),"mm"),
             font = list(family = "sans", size = 12, face = "plain"),
             separator = list(h=FALSE, v=TRUE), box = FALSE,
             core = list(bg=bg, fg=fg, parse=TRUE,
             separator=separator, box=box, padding=padding,
             just=just, hjust=hjust, vjust=vjust, font=font),
             col_header = inplace(core, parse=FALSE,
             font=inplace(font, face="bold")),
             row_header = inplace(col_header, just=c("right", "center"),
             font = inplace(font, face="italic"),
             hjust=1, padding=unit(c(4,4),"mm"))){
        ll <- list(bg=bg, fg=fg,
                   font=font, separator=separator, box=box,
                   core=core, row_header=row_header, col_header=col_header)
        dots <- list(...)
        Reduce(modifyList, c(list(ll), dots))
    }
## Two ways to change both the size and face of the col_header font

## (1) Pass in both modifications in a single list
theme1 = table_theme(list(col_header=list(font=list(size=10, face="italic"))))
## (2) Pass each modification in in a list of its own
theme2 = table_theme(list(col_header=list(font=list(face="italic"))),
                    list(col_header=list(font=list(size=10))))
## Check that they both have the same effect
identical(theme1, theme1)
# [1] TRUE

尽管传递list(col_header=list(font=list(face="italic")))比完成col_header$font$face <- "italic"更加冗长,但结构完全同源,因此前者应该不难掌握。

09-05 12:24