什么是 NULL 环境?
What is the NULL environment?
如果我检查 sqrt
函数的环境,我得到 NULL
:
> environment(sqrt)
NULL
另一方面,基础包中的函数split
具有环境:
> environment(split)
<environment: namespace:base>
为什么这两个函数有不同的环境,NULL环境是什么意思?
sqrt
是原始函数,没有 R 代码。每 Hadley Wickham's advanced R 页:
Primitive functions
There is one exception to the rule that functions have three components. Primitive functions, like sum()
, call
C code directly with .Primitive(
) and contain no R code. Therefore
their formals()
, body()
, and environment()
are all NULL
.
如果我检查 sqrt
函数的环境,我得到 NULL
:
> environment(sqrt)
NULL
另一方面,基础包中的函数split
具有环境:
> environment(split)
<environment: namespace:base>
为什么这两个函数有不同的环境,NULL环境是什么意思?
sqrt
是原始函数,没有 R 代码。每 Hadley Wickham's advanced R 页:
Primitive functions
There is one exception to the rule that functions have three components. Primitive functions, like
sum()
, call C code directly with.Primitive(
) and contain no R code. Therefore theirformals()
,body()
, andenvironment()
are allNULL
.