本文介绍了如何在r中的函数内将参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个函数,它涉及带有大量参数的基础 R 中的其他函数.例如(真正的函数要长得多):
I am writing function that involve other function from base R with alot of arguments. For example (real function is much longer):
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"), ...)
我想使用这些参数而不在 myfunction 中列出它们.
I want to use these arguments without listing them inside 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))
推荐答案
尝试用三个点代替四个点,并在顶级函数中添加省略号参数:
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)
}
这篇关于如何在r中的函数内将参数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!