繁体   English   中英

R 将“空白”或“缺失”arguments 传递到 package ZC1C425268E18385D1AB507 与 elseC 语句

[英]R pass “blank” or “missing” arguments to package function with if else statement

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

我想将参数设为可选,但参数本身没有我可以收集的 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很脆弱...请参阅缺少参数 object 的脆弱性部分的?missing_arg 这有效:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM