當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R call 函數調用

R語言 call 位於 base 包(package)。

說明

創建或測試 mode "call" (或 "(" ,請參閱詳細信息)的對象。

用法

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

參數

name

命名要調用的函數的非空字符串。

...

參數是調用的一部分。

x

任意的R對象。

細節

call

返回未計算的函數調用,即由應用於給定參數的命名函數組成的未計算的表達式(name 必須是給出要調用的函數名稱的字符串)。請注意,盡管該調用未求值,但參數 ... 已求值。

call 是原語,因此第一個參數被視為 name ,其餘參數作為構造調用的參數:如果命名第一個參數,則名稱必須部分匹配 name

is.call

用於確定 x 是否是一個調用(即模式 "call""(" )。注意

  • is.call(x) 嚴格等同於 typeof(x) == "language"

  • is.language() 對於調用也適用(但對於 symbolexpression 也適用,其中 is.call() 為 false)。

as.call(x)

模式 "list" 的對象可以強製為模式 "call" 。列表的第一個元素成為調用的函數部分,因此應該是函數或函數的名稱(作為符號;字符串不行)。

如果您考慮使用 as.call(<string>) ,請考慮使用 str2lang(*) ,它是 parse(text=*) 的高效版本。請注意,call()as.call() 在適用時比這些基於 parse() 的方法更可取。

這三個都是primitive 函數。

as.call 是通用的:您可以編寫方法來處理特定類的對象,請參閱 InternalMethods

警告

call 不應被用來嘗試規避對 .Internal 和其他非 API 調用的使用限製。

例子

is.call(call) #-> FALSE: Functions are NOT calls

## set up a function call to round with argument 10.5
cl <- call("round", 10.5)
is.call(cl) # TRUE
cl
identical(quote(round(10.5)), # <- less functional, but the same
          cl) # TRUE
## such a call can also be evaluated.
eval(cl) # [1] 10

class(cl) # "call"
typeof(cl)# "language"
is.call(cl) && is.language(cl) # always TRUE for "call"s

A <- 10.5
call("round", A)        # round(10.5)
call("round", quote(A)) # round(A)
f <- "round"
call(f, quote(A))       # round(A)
## if we want to supply a function we need to use as.call or similar
f <- round
## Not run: call(f, quote(A))  # error: first arg must be character
(g <- as.call(list(f, quote(A))))
eval(g)
## alternatively but less transparently
g <- list(f, quote(A))
mode(g) <- "call"
g
eval(g)
## see also the examples in the help for do.call

參考

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

也可以看看

do.call 用於通過名稱和參數列表調用函數; Recall 用於函數的遞歸調用;進一步is.languageexpressionfunction

從字符生成 call 等: str2langparse

相關用法


注:本文由純淨天空篩選整理自R-devel大神的英文原創作品 Function Calls。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。