R中match.call(),call(), do.call()和 .Call() 函数

match.call {base} 

Description

match.call returns a call in which all of the specified arguments are specified by their full names.

Usage

match.call(definition = sys.function(sys.parent()),
           call = sys.call(sys.parent()),
           expand.dots = TRUE,
           envir = parent.frame(2L))

Arguments

definition

a function, by default the function from which match.call is called. See details.

call

an unevaluated call to the function specified by definition, as generated by call.

expand.dots

logical. Should arguments matching ... in the call be included or left as a ... argument?

envir

an environment, from which the ... in call are retrieved, if any.

 这个函数会在一般都是写在另外一个函数里边。其会返回其宿主函数体的入参的匹配关系。

具体看下面的代码:

expand_FALSE1<-function (formula, data, subset, na.action = na.pass, weights, 
    offset, cluster, strata, scores = NULL, yx = c("none", "matrix"), 
    ytype = c("vector", "data.frame", "matrix"), nmax = c(yx = Inf, 
        z = Inf), ...) 
{
     cl <- match.call(expand.dots = FALSE)
     print('=======================================')
     print(cl)
     print('=======================================')

     clnam <- names(cl)[-1L]
     print(clnam)
     print(length(clnam))
     

}
expand_FALSE1(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)

[1] "======================================="
expand_FALSE(formula = 1, data = 2, subset = 3, na.action = 4, 
    weights = 5, offset = 6, cluster = 7, strata = 8, scores = 9, 
    yx = 10, ytype = 11, nmax = 12, ... = pairlist(13, 14, 15))
[1] "======================================="
 [1] "formula"   "data"      "subset"    "na.action"
 [5] "weights"   "offset"    "cluster"   "strata"   
 [9] "scores"    "yx"        "ytype"     "nmax"     
[13] "..."      
[1] 13

expand_FALSE2 <-function (formula, data, subset, na.action = na.pass, weights, 
    offset, cluster, strata, scores = NULL, yx = c("none", "matrix"), 
    ytype = c("vector", "data.frame", "matrix"), nmax = c(yx = Inf, 
        z = Inf), ...) 
{
     cl <- match.call(expand.dots = TRUE)
     print('=======================================')
     print(cl)
     print('=======================================')

     clnam <- names(cl)[-1L]
     print(clnam)
     print(length(clnam))
     

}
expand_FALSE2(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)


[1] "======================================="
expand_FALSE2(formula = 1, data = 2, subset = 3, na.action = 4, 
    weights = 5, offset = 6, cluster = 7, strata = 8, scores = 9, 
    yx = 10, ytype = 11, nmax = 12, 13, 14, 15)
[1] "======================================="
 [1] "formula"   "data"      "subset"    "na.action" "weights"  
 [6] "offset"    "cluster"   "strata"    "scores"    "yx"       
[11] "ytype"     "nmax"      ""          ""          ""         
[1] 15


expand_FALSE3 <-function (formula, data, subset, na.action = na.pass, weights, 
    offset, cluster, strata, scores = NULL, yx = c("none", "matrix"), 
    ytype = c("vector", "data.frame", "matrix"), nmax = c(yx = Inf, 
        z = Inf), ...) 
{
     cl <- match.call()

     print('=======================================')
     print(cl)
     print('=======================================')

     clnam <- names(cl)[-1L]
     print(clnam)
     print(length(clnam))
     

}
expand_FALSE3(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15)

[1] "======================================="
expand_FALSE3(formula = 1, data = 2, subset = 3, na.action = 4, 
    weights = 5, offset = 6, cluster = 7, strata = 8, scores = 9, 
    yx = 10, ytype = 11, nmax = 12, 13, 14, 15)
[1] "======================================="
 [1] "formula"   "data"      "subset"    "na.action" "weights"  
 [6] "offset"    "cluster"   "strata"    "scores"    "yx"       
[11] "ytype"     "nmax"      ""          ""          ""         
[1] 15

call{base}

Description

Create or test for objects of mode "call" (or "(", see Details).

Usage

call(name, ...)
is.call(x)
as.call(x)

Arguments

name

a non-empty character string naming the function to be called.

...

arguments to be part of the call.

x

an arbitrary R object.

call 不进行计算只是把参数值放入到调用的函数作为表达式输出。与quote函数相似,如果需要计算值得话,需调用eval()函数。

## set up a function call to round with argument 10.5
cl <- call("round", 10.5)
is.call(cl)
# TRUE
cl
#round(10.5)
identical(quote(round(10.5)), cl)

# TRUE
## such a call can also be evaluated.
eval(cl) 
# [1] 10


do.call {base}

do.call constructs and executes a function call from a name or a function and a list of arguments to be passed to it.

Usage

do.call(what, args, quote = FALSE, envir = parent.frame())

Arguments

what

either a function or a non-empty character string naming the function to be called.

args

list of arguments to the function call. The names attribute of args gives the argument names.

quote

a logical value indicating whether to quote the arguments.

envir

an environment within which to evaluate the call. This will be most useful if what is a character string and the arguments are symbols or quoted expressions.

do.call("complex", list(imag = 1:3))
# [1] 0+1i 0+2i 0+3i

## if we already have a list (e.g., a data frame)
## we need c() to add further arguments
tmp <- expand.grid(letters[1:2], 1:3, c("+", "-"))
#> tmp
   Var1 Var2 Var3
1     a    1    +
2     b    1    +
3     a    2    +
4     b    2    +
5     a    3    +
6     b    3    +
7     a    1    -
8     b    1    -
9     a    2    -
10    b    2    -
11    a    3    -
12    b    3    -

do.call("paste", c(tmp, sep = ""))
# [1] "a1+" "b1+" "a2+" "b2+" "a3+" "b3+" "a1-" "b1-" "a2-" "b2-" "a3-" "b3-"

do.call(paste, list(as.name("A"), as.name("B")), quote = TRUE)
#[1] "A B"

.Call() 函数 

Functions to pass R objects to compiled C/C++ code that has been loaded into R.

通过R把C/C++代码导入到R中。

    .Call(.NAME, ..., PACKAGE)
.External(.NAME, ..., PACKAGE)
.Call(lme4:::glmerLaplace, rho$pp$ptr(), rho$resp$ptr(), nAGQinit, control$tolPwrss, maxit, verbose)

data <- .External2(stats:::C_modelframe, formula, rownames, variables, 
        varnames, extras, extranames, subset, na.action)

单独运行时必须要添加程序包名称和“:::”

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值