![](/img/trans.png)
[英]How to evaluate one argument of a function call using another argument of that call, and an object created inside the function environment in R?
[英]R get the environment created by a function at the call
我希望在運行時通過函數創建環境而不修改函數源(即從函數外部),是否可能?
fn=function()
{#Here a new environment is created at each call, how to get it ?
#This environment can be access with environment() but only (to what I know)
#from inside the function
...
}
我想要這樣的東西:
env=some_function(fn())
其中env是fn在調用時創建的環境id。
您可以跟蹤函數以將調用環境綁定到全局環境中的符號:
fn <- function() {x <- 2; 1}
trace(fn, quote(efn <<- environment()), at = 1)
fn()
#Tracing fn() step 1
#[1] 1
untrace(fn)
efn$x
#[1] 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.