如何将参数传递到函数中的函数中 [英] how to pass arguments into function within a function in r

查看:106
本文介绍了如何将参数传递到函数中的函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写的函数涉及到来自基础R的其他函数以及很多参数。例如(真实函数更长):

  myfunction<  -  function(dataframe,Colv = NA){$ b $ (矩阵,Colv = Colv)
返回(出)
}

数据(矩阵,Colv = Colv) mtcars)

myfunction(mtcars,Colv = NA)

许多参数可以传递给:

  heatmap(x,Rowv = NULL,Colv = if(symm)Rowvelse NULL,
distfun = dist,hclustfun = hclust,
reorderfun =函数(d,w)reorder(d,w),
add.expr,symm = FALSE,revC =相同(Colv ,Rowv),
scale = c(row,column,none),na.rm = TRUE,
margin = 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))
myfunction中的错误(mtcars,Colv = NA,col = topo.colors(16) ):
unused argument(s)(col = topo.colors(16))

我尝试了以下但不起作用:
$ b $ pre $ myfunction< - function(dataframe,Colv = NA){
矩阵< - as.matrix(数据帧)
out< - 热图(矩阵,Colv = Colv,....)
返回(出)
}
数据mtcars)

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


<



<$ p

解决方案

尝试使用三个点而不是四个,并将省略号参数添加到顶层函数中: (数据帧,Colv = NA,...){
矩阵< - as.matrix(数据帧)
out< - 热图(矩阵,Colv = Colv,...)
r eturn(out)
}


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

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

这篇关于如何将参数传递到函数中的函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆