R 将 "blank" 或 "missing" 参数传递给带有 if else 语句的包函数

R pass "blank" or "missing" arguments to package function with if else statement

我的具体问题是将 heatmap.2 与闪亮的服务器一起使用,但我想这个问题适用于更多地方。

我想将一个参数设为可选,但据我所知,该参数本身没有 NULL/FALSE 默认值。我正在尝试使用 heatmap.2 中的 RowSideColors 和 ColSideColors 选项来做到这一点。

似乎定义参数并将其留空有效,以及通过 rlang 创建 missing_arg。但是当你将其中任何一个合并到 if 语句中时,heatmap.2 不喜欢它。

有什么想法吗?

library(rlang)
library(gplots)

data(mtcars)
x  <- as.matrix(mtcars)

missing <-missing_arg()
row_clusters <- 0

#works
heatmap.2(x, RowSideColors= )
heatmap.2(x, RowSideColors= missing )

#doesn't work
heatmap.2(x, RowSideColors= FALSE)
heatmap.2(x, RowSideColors= NULL)
heatmap.2(x, RowSideColors= NA)
heatmap.2(x, RowSideColors= NaN)
heatmap.2(x, if(row_clusters == 0)RowSideColors= )
heatmap.2(x, if(row_clusters == 0)RowSideColors= missing )
heatmap.2(x, RowSideColors= if(row_clusters == 0){missing}else{})
heatmap.2(x, RowSideColors= if(row_clusters == 0) missing else missing)
etc...

对于上下文,这里是 heatmap.2 中 RowSideColors 和 ColSideColors 在后台发生的事情:

if(!missing(ColSideColors)) { ## add middle row to layout
        if(!is.character(ColSideColors) || length(ColSideColors) != nc)
          stop("'ColSideColors' must be a character vector of length ncol(x)")

我很惊讶 heatmap.2(x, RowSideColors= if(row_clusters == 0) missing else missing) 没有工作...我认为问题是 missing_arg 是脆弱的...请参阅缺少参数对象的脆弱性 ?missing_arg 部分。这有效:

rowside_arg <- if(row_clusters == 0) missing_arg() else missing_arg()
heatmap.2(x, RowSideColors = arg)