如何在r中的函数内将参数传递给函数

时间:2021-11-29 20:36:50

I am writing function that involve other function from base R with alot of arguments. For example (real function is much longer):

我正在编写涉及来自基本R的其他函数的函数和许多参数。例如(实际功能更长):

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

data(mtcars)

myfunction (mtcars, Colv = NA)

The heatmap has many arguments that can be passed to:

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

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

I want to use these arguments without listing them inside myfunction.

我想使用这些参数而不在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))

I tried the following but do not work:

我尝试了以下但不起作用:

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

1 个解决方案

#1


27  

Try three dots instead of four, and add the ellipsis argument to the top level function:

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

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

#1


27  

Try three dots instead of four, and add the ellipsis argument to the top level function:

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

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