2012-06-17 67 views
22

我正在寫函數,它涉及來自基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)) 

回答

24

嘗試三個點,而不是四個,和省略號參數添加到頂層功能:

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

我想補充一個選項(或者添加混淆),可以通過在主函數'dotfuns <-list(...,other_variable)'中設置一個變量並將其傳遞給後續函數來擴充'...'的內容。做這件事需要您自擔風險:-) –

+0

@CarlWitthoft對不起,我不明白你的意思,可能會解開你的解決方案(也許作爲答案) – jon

+0

這不是你的具體問題的答案,只是對其他事情的評論,可以用'...'參數集合來完成。 –

相關問題