Wrapper for a function relying on non-standard evaluation in R

馋奶兔 提交于 2019-12-01 21:28:24

With a function definition without ..., I get a different error:

mytable <- function(formula,
                    data,
                    subset) ftable(formula = formula,
                                   data = data,
                                   subset = subset)

mytable(formula = wool + tension ~ breaks,
        data = warpbreaks,
        subset = breaks < 20)

 Error in xj[i] : invalid subscript type 'closure'

This error led me to ressources I havent found before.

Some threads led me to:

# function 1
mytable <- function(...) {
    mc <- match.call()
    mc["exclude"] <- list(NULL)
    do.call(what = ftable,
            args = as.list(x = mc[-1]))
    #etc
}

The write.csv family and lm source code led me to:

# function 2
mytable <- function(...) {
    mc <- match.call()
    mc[[1]] <- quote(expr = ftable)
    mc["exclude"] <- list(NULL)
    eval(expr = mc)
    # etc
}

However, I am looking for pro and cons of both methods (function 1 and function 2), because I do not know if a method is to be favored. So far I just found that do.call might be slower.

More importantly, these methods led my to another issue: I can not use my wrapper with lapply and with anymore.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!