[英]How to check if a function is anonymous in R
I want to write a function that distinguishes if a function is being sourced from a package or set up anonymously in a case where these are the only 2 possibilities (constraints of a one-liner). 我想编写一个函数来区分是从包中获取函数还是在仅有的两种可能性(单线约束)的情况下匿名设置函数。
If it's the latter - let's call it a variable anonymous.function
- I'd want to 'get it out' of the (function(parameters){inner_workings})
closure as function(){}
by eval(parse(text = call(anonymous.function()))
如果是后者-让我们把它变anonymous.function
-我想的要“把它弄出来” (function(parameters){inner_workings})
闭包function(){}
通过eval(parse(text = call(anonymous.function()))
typeof(package::function)
returns "closure" typeof(package::function)
返回“关闭” typeof((function(parameters) {inner_workings}))
likewise returns "closure" typeof((function(parameters) {inner_workings}))
同样返回“闭包” Should I distinguish the two by comparing environment
, or namespace
? 我应该通过比较environment
还是namespace
来区分两者?
environment(package::function)
returns <environment: namespace:package>
environment(package::function)
返回<environment: namespace:package>
environment((function)parameters) {inner_workings}))
returns <environment: R_GlobalEnv>
environment((function)parameters) {inner_workings}))
返回<environment: R_GlobalEnv>
I'm hesitant to say that I'll only assume it's anonymous if environment is GlobalEnv, as the stack could be traversed below Global if I understand correctly in some situations, eg if I'm running this function within a function, so I'd check if environment is a namespace. 我很犹豫地说,如果环境是GlobalEnv,我只会假设它是匿名的,因为如果在某些情况下我理解正确,例如,如果我在一个函数中运行此函数,则可以在Global下遍历堆栈。 d检查环境是否为名称空间。
Can anyone tell me how I can check for a namespace? 谁能告诉我如何检查名称空间? I can see it in the reported string as <environment: namespace:...>
but can't seem to access that distinction. 我可以在报告的字符串中以<environment: namespace:...>
看到它,但似乎无法访问该区别。 The only distinction I can see is that a namespace environment isn't hashed - that's only observed upon failing to run env.profile()
for an unhashed environment... 我能看到的唯一区别是名称空间环境没有被散列-仅在未运行的环境中运行env.profile()
时才观察到...
Surely there's a simple way to check for anonymous functions without a try/catch statement! 当然,有一种简单的方法可以在没有try / catch语句的情况下检查匿名函数! :-) Advice would be greatly appreciated, the R documentation seems to run dry at this level. :-)建议将不胜感激,R文档似乎在此级别上干dry了。
Edit ideally accessing the data structure, ie without grepl
ing a string! 编辑以理想的方式访问数据结构,即无需grepl
字符串!
Does packageName
work for you? packageName
对您packageName
吗? I was unsure about the desired output of a function like f1 <- mean
. 我不确定像f1 <- mean
这样的函数的期望输出。 See below for my version. 请参阅下面的我的版本。
is_package_fun <- function(fun)
!is.null(packageName(environment(fun)))
# defining functions
f1 <- mean
f2 <- function(x) mean(x)
# checking
is_package_fun(mean)
## [1] TRUE
is_package_fun(f1) # not sure about desired output...
## [1] TRUE
is_package_fun(f2)
## [1] FALSE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.