Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass arguments into function within a function

I am writing function that involve other function from base R with a lot 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"), ...) 

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)) 
like image 245
jon Avatar asked Jun 17 '12 15:06

jon


People also ask

Can we pass arguments to a function?

Passing arguments to function is a very important aspect of C++ programming. Arguments refer to values that can be passed to a function. Furthermore, this passing of arguments takes place for the purpose of being used as input information.

Can you pass a function as a parameter of another function in JavaScript?

Conclusion. Functions in the functional programming paradigm can be passed to other functions as parameters. These functions are called callbacks. Callback functions can be passed as arguments by directly passing the function's name and not involving them.


1 Answers

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) } 
like image 120
joran Avatar answered Oct 16 '22 13:10

joran