36

我正在编写函数,该函数涉及来自基本 R 的其他函数,并带有很多参数。例如(实际功能要长得多):

myfunction <- function (dataframe, Colv = NA) { 
matrix <- as.matrix (dataframe) 
out <- heatmap(matrix, Colv = Colv)
return(out)
}

data(mtcars)

myfunction (mtcars, Colv = NA)

热图有许多参数可以传递给:

heatmap(x, Rowv=NULL, Colv=if(symm)"Rowv" else NULL,
        distfun = dist, hclustfun = hclust,
        reorderfun = function(d,w) reorder(d,w),
        add.expr, symm = FALSE, revC = identical(Colv, "Rowv"),
        scale=c("row", "column", "none"), na.rm = TRUE,
        margins = c(5, 5), ColSideColors, RowSideColors,
        cexRow = 0.2 + 1/log10(nr), cexCol = 0.2 + 1/log10(nc),
        labRow = NULL, labCol = NULL, main = NULL,
        xlab = NULL, ylab = NULL,
        keep.dendro = FALSE, verbose = getOption("verbose"), ...)

我想使用这些参数而不在 myfunction 中列出它们。

myfunction (mtcars, Colv = NA, col = topo.colors(16))
Error in myfunction(mtcars, Colv = NA, col = topo.colors(16)) : 
  unused argument(s) (col = topo.colors(16))

我尝试了以下但不工作:

myfunction <- function (dataframe, Colv = NA) { 
matrix <- as.matrix (dataframe) 
out <- heatmap(matrix, Colv = Colv, ....)
return(out)
}
data(mtcars)

myfunction (mtcars, Colv = NA, col = topo.colors(16))
4

2 回答 2

40

尝试三个点而不是四个点,并将省略号参数添加到顶级函数:

myfunction <- function (dataframe, Colv = NA, ...) { 
    matrix <- as.matrix (dataframe) 
    out <- heatmap(matrix, Colv = Colv, ...)
    return(out)
}
于 2012-06-17T15:37:17.377 回答
0

试试这个:

  • globalenv 中热图的参数:
argsHeat = alist( <arguments for heatmap>)
  • 不要在里面调用 heatmap() myfunction,而是这样做
out = do.call('heatmap', args = argsHeat)
于 2022-02-16T19:22:40.117 回答